Ad

How To Show Error On Email Form Field If Same Email Exist In Firebase Users

I have a Form with email TextFormField I'm validating their user input data before they signup and showing errors accordingly, as all the fields are valid calling a function to signUp with firebase and if the same email exist in users it gives me error "ERROR_EMAIL_ALREADY_IN_USE", I just want to show an error below the email TextFormField that this email is already in use.

onPressed: () {
    if (_formKey.currentState.validate()) {
        print('form is valid');
        AuthService()
            .signUp(emailController.text,
                passwordFirstField.text).catchError((err) => {

            })
            .then((userId) {
                AuthService().signUpAsAClient(
                    userId, usernameController.text);
            });
        Navigator.of(context).pushNamed(
            '/professionals_list',
        );
    } else {
        print('form is not valid');
    }
}

need to show an error That email is already in use from firebase.

Ad

Answer

Define a String variable on the to of widget like this

String _errorEmail;

Here is the code of textfield

TextField(
                        controller: _tecEmail,
                        style: TextStyle(color: Colors.white, fontSize: 18.0),
                        decoration: InputDecoration(
                            errorText: _errorEmail,
                            errorStyle:
                                TextStyle(color: Colors.red, fontSize: 14.0),
                            labelText: "Email Address",
                            labelStyle: TextStyle(
                                fontSize: 18.0, color: Colors.white70),
                            enabledBorder: UnderlineInputBorder(
                                borderSide: BorderSide(color: Colors.white)),
                            focusedBorder: UnderlineInputBorder(
                              borderSide:
                                  BorderSide(color: Constants.appThemeColor),
                            ),
                            contentPadding: const EdgeInsets.only(
                                left: 10, right: 10, top: 10, bottom: 10)),
                        keyboardType: TextInputType.emailAddress,
                      ),

add the error message if the emaild already exist

setState(() {
      if (//condition or check if email exist) {
      _errorEmail = "emailid already exist";
      return;
    }
    _errorEmail = null;
    });
Ad
source: stackoverflow.com
Ad