0

I am very new to flutter, I already connect the Firebase function with sign in button. It work perfectly, the only thing I wanted to do is to add Error message if the email and password is not found, not registered etc, for example there is no known id inside the firebase of the user account. It will show 'User not found", invalid email or password etc.

Container(
                padding: const EdgeInsets.symmetric(horizontal: 40),
                child: Column(
                  children: [
                    Padding(
                      padding: const EdgeInsets.symmetric(vertical: 15.0),
                      child: Container(
                        decoration: BoxDecoration(
                            color: Colors.grey[500].withOpacity(0.5),
                            borderRadius: BorderRadius.circular(16)),
                        child: TextFormField(
                          onChanged: (value) {
                            setState(() {
                              _email = value.trim();
                            });
                          },
                          decoration: InputDecoration(
                            contentPadding:
                                const EdgeInsets.symmetric(vertical: 20.0),
                            border: InputBorder.none,
                            hintText: 'Email',
                            hintStyle: TextStyle(
                                fontSize: 20.0, color: Colors.white),
                            prefixIcon: Padding(
                              padding: const EdgeInsets.symmetric(
                                  horizontal: 20.0),
                              child: Icon(
                                FontAwesomeIcons.solidEnvelope,
                                color: Colors.white,
                                size: 20.0,
                              ),
                            ),
                          ),
                          style: TextStyle(
                              fontSize: 20.0, color: Colors.white),
                          keyboardType: TextInputType.emailAddress,
                          textInputAction: TextInputAction.next,
                        ),
                      ),
                    ),
                    Container(
                      decoration: BoxDecoration(
                          color: Colors.grey[500].withOpacity(0.5),
                          borderRadius: BorderRadius.circular(16)),
                      child: TextFormField(
                        onChanged: (value) {
                          setState(() {
                            _password = value.trim();
                          });
                        },
                        decoration: InputDecoration(
                            contentPadding:
                                const EdgeInsets.symmetric(vertical: 20.0),
                            border: InputBorder.none,
                            hintText: 'Password',
                            hintStyle: TextStyle(
                                fontSize: 20.0, color: Colors.white),
                            prefixIcon: Padding(
                              padding: const EdgeInsets.symmetric(
                                  horizontal: 20.0),
                              child: Icon(
                                FontAwesomeIcons.lock,
                                color: Colors.white,
                                size: 20.0,
                              ),
                            ),
                            suffixIcon: Icon(
                              FontAwesomeIcons.eye,
                              color: Colors.white,
                              size: 20.0,
                            )),
                        obscureText: true,
                        style:
                            TextStyle(fontSize: 20.0, color: Colors.white),
                        keyboardType: TextInputType.name,
                        textInputAction: TextInputAction.done,
                      ),
                    ),
                    SizedBox(
                      height: 20.0,
                    ),
                    RoundedButton(
                        text: "Login",
                        color: Colors.grey[600],
                        press: () async {
                          await auth.signInWithEmailAndPassword(
                              email: _email, password: _password);
                          Navigator.of(context).pushReplacement(
                              MaterialPageRoute(
                                  builder: (context) => WelcomeScreen()));
                        }),

Any help is appreciated. Thank you. im very new to stack overflow too. sorry if my question format wrong

1 Answer 1

2

You should always surround your sign in code with a try/catch statement:

try {
    await auth.signInWithEmailAndPassword(
       email: _email, password: _password);
} on FirebaseAuthException catch (e) {
    //here you get the error messages and do stuff accordingly
    if(e.code == 'wrong-password') {
        //handle wrong password like in an alertdialog
    }
}

There are different codes that will be returned when an error occurs:

  • 'user-not-found'
  • 'invalid-email'
  • 'user-disabled'
  • 'operation-not-allowed'

And so on. You can check out the full error code list on this site:

Firebase erorrs

Sign up to request clarification or add additional context in comments.

2 Comments

I need to wrap it with only the sign in button right?
you should only wrap the firebase function itself in a try/catch statement. You can go ahead and paste the code to your onPressed from the button. If this is the solution, please mark as answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.