skip to Main Content

I have a try .. catch block when loggin in or signing up with FirebaseAuth.instance

This is the code that I have.

 try {
   //... code here
} on PlatformException catch (err) {
  var message = 'An error occured, please check your credentials.';

  if (err.message != null) {
    message = err.message!;
  }

  ScaffoldMessenger.of(ctx).showSnackBar(
    SnackBar(
      content: Text(message),
      backgroundColor: Theme.of(ctx).errorColor,
    ),
  );
  // other codes here

} catch (err) {
  print(err);
}

The problem now is that if I’m encountering an error like The email address is badly formatted. or he email address is already in use by another account, this is not going inside the on PlatformException catch (err) block and its goin in the catch block instead.

How can I make sure that the types of errors I mentioned above should be executed in the on PlatformException block?

2

Answers


  1. Chosen as BEST ANSWER

    Alright! I have resolved it by using on FirebaseAuthException instead of on PlatformException. Hopefully this will help others who will be experiencing the same problem in the future.


  2. Try with this hope you’ll get the solution!

       try {
           //... code here
        } on FirebaseAuthException catch (e) {
          String errorMessage = AuthExceptionHandler.handleException(e);
          errorSnackBar(content: errorMessage);
        } catch (e) {
            print(e.toString());
        }
    

    AuthExceptionHandler class for managing error message according error code.

    class AuthExceptionHandler {
      static handleException(e) {
        AuthResultStatus status;
        switch (e.code) {
          case "invalid-email":
            status = AuthResultStatus.invalidEmail;
            break;
          case "wrong-password":
            status = AuthResultStatus.wrongPassword;
            break;
          case "user-not-found":
            status = AuthResultStatus.userNotFound;
            break;
          case "user-disabled":
            status = AuthResultStatus.userDisabled;
            break;
          case "too-many-requests":
            status = AuthResultStatus.tooManyRequests;
            break;
          case "operation-not-allowed":
            status = AuthResultStatus.operationNotAllowed;
            break;
          case "email-already-in-use":
            status = AuthResultStatus.emailAlreadyExists;
            break;
          default:
            status = AuthResultStatus.undefined;
        }
        return generateExceptionMessage(status);
      }
    
      ///
      /// Accepts AuthExceptionHandler.errorType and set message according error
      static generateExceptionMessage(exceptionCode) {
        String errorMessage;
        switch (exceptionCode) {
          case AuthResultStatus.invalidEmail:
            errorMessage = StringConstants.emailAddressMalformed.tr;
            break;
          case AuthResultStatus.wrongPassword:
            errorMessage = StringConstants.wrongPassword.tr;
            break;
          case AuthResultStatus.userNotFound:
            errorMessage = StringConstants.userNotExist.tr;
            break;
          case AuthResultStatus.userDisabled:
            errorMessage = StringConstants.userDisable.tr;
            break;
          case AuthResultStatus.tooManyRequests:
            errorMessage = StringConstants.manyRequestTryAfterSomeTime.tr;
            break;
          case AuthResultStatus.operationNotAllowed:
            errorMessage = StringConstants.signInNotEnable.tr;
            break;
          case AuthResultStatus.emailAlreadyExists:
            errorMessage = StringConstants.emailAlreadyExist.tr;
            break;
          default:
            errorMessage = StringConstants.undefinedErrorHappened.tr;
        }
    
        return errorMessage;
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search