Home > OS >  Firebase on PlatformException Error Not working
Firebase on PlatformException Error Not working

Time:12-02

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?

CodePudding user response:

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;
  }
}

CodePudding user response:

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.

  • Related