0
votes

i have developed a login page.but i don't know how to catch firebase auth exceptions on flutter and display them.i want to display something email doesn't exist, password didn't match.

Here's my code:

Future<void> login() async{
    final  formState = _formkey.currentState;
    if(formState.validate()){
      formState.save();
      try{

        final FirebaseUser user = (await FirebaseAuth.instance.signInWithEmailAndPassword(email: _username, password: _password)).user;

          Navigator.push(context, MaterialPageRoute(builder: (context) => Admin()));
      }catch(e){
        print(e.message);
      }

Can someone help me finding a way to validate the login please.

2

2 Answers

2
votes

You can use a Toast to display the error:

# add this line to your dependencies
toast: ^0.1.5

Then in the dart file import it:

import 'package:toast/toast.dart';

Then inside the catch:

 }catch(e){
        print(e.message);
    Toast.show(e.message, context, duration: Toast.LENGTH_SHORT, gravity:  Toast.BOTTOM);    
 }

https://pub.dev/packages/toast

You don't have to check if the email is wrong or not, the signInWithEmailAndPassword already does that for you and will return an error if the authentication was wrong.

0
votes

You could just display the e.message from your catch block to the user - if all you want is to display the message.