Home > Blockchain >  FirebaseAuthException catches error but the app still stops working
FirebaseAuthException catches error but the app still stops working

Time:04-04

I'm making a login/sign up and in the signup when i try to signup with the same email the app stoped, i added this line

case "email-already-exists":
            errorMessage = "The email is already being used.";
            break;

The problem is, it still doesn't work, the message shows up but the app still stop working. (but the message it's not the same

enter image description here

and shows this error enter image description here

this is the code that i'm using for authentication

void signUp(String email, String password) async {
    if (_formKey.currentState!.validate()) {
      try {
        await _auth
            .createUserWithEmailAndPassword(email: email, password: password)
            .then((value) => {postDetailsToFirestore()})
            .catchError((e) {
          _toastInfo(e!.message);
        });
      } on FirebaseAuthException catch (error) {
        switch (error.code) {
          case "invalid-email":
            errorMessage = "Your email address appears to be malformed.";
            break;
          case "wrong-password":
            errorMessage = "Your password is wrong.";
            break;
          case "user-not-found":
            errorMessage = "User with this email doesn't exist.";
            break;
          case "user-disabled":
            errorMessage = "User with this email has been disabled.";
            break;
          case "too-many-requests":
            errorMessage = "Too many requests";
            break;
          case "operation-not-allowed":
            errorMessage = "Signing in with Email and Password is not enabled.";
            break;
          case "email-already-exists":
            errorMessage = "The email is already being used.";
            break;
          default:
            errorMessage = "An undefined Error happened.";
        }
        _toastInfo(errorMessage!);
        print(error.code);
      }
    }
  }

CodePudding user response:

The error could be caused by your code here:

.catchError((e) {
          _toastInfo(e!.message);
        });

.catchError() returns a future of the type Future<Map<dynamic, dynamic>>, but within your curly braces, you're not explicitly returning a map. Could you implement this is another way?

CodePudding user response:

The errors doesn't lay on "FirebaseAuthException", but as the error message tells you: "The error handler of Future.catchError... "

 await _auth
            .createUserWithEmailAndPassword(email: email, password: password)
            .then((value) => {postDetailsToFirestore()})
            .catchError((e) {
          _toastInfo(e!.message);
        });

2 things are not done properly.

  1. You're using both await and then, which are not the right way of dealing with asynchronous functions. In this case they both do the same thing, which is not good. Read here to learn more about Async functions - Here the link for the official Flutter Documentation
  1. Your catchError should return a value of the future type. In your case, your function should return a "UserCredential" data type.

Here's the way I deal with UserAuth

  /// [Creating account using Email & Password]
  Future<User?> createAccount(String email, String password) async {
    try {
      UserCredential userCredential = await FirebaseAuth.instance
          .createUserWithEmailAndPassword(email: email, password: password);
      return userCredential.user;
    } on FirebaseAuthException catch (e) {
      // Here you manage the errors
      return null;
    }
  }
  • Related