3

I am developing a login system, using firebase, flutter and vscode.

I would like to know how to handle exceptions generated by Firebase. If EMAIL is already registered.

Currently generating an error:

Exception has occurred.
PlatformException (PlatformException(ERROR_EMAIL_ALREADY_IN_USE, The email address is already in use by another account., null))

If the email is already registered, I want to inform the user.

CODE:

Future<void> signUp({@required Map<String, dynamic> userData,@required String pass,@required VoidCallback onSuccess,@required VoidCallback onFail}) async{
    isLoading = true;
    notifyListeners();

    _auth.createUserWithEmailAndPassword(
      email: userData["email"],
      password: pass
    ).then((user) async{
      firebaseUser = user;
      await _saveUserData(userData);
      onSuccess();
      isLoading = false;
      notifyListeners();
    }).catchError((e){

      print(e);
      onFail();
      isLoading = false;
      notifyListeners();
    });

  }
1
  • Does anyone have a solution for this? Commented Apr 22, 2019 at 22:56

2 Answers 2

2

If you want to perform subsequent operations when ERROR_EMAIL_ALREADY_IN_USE is emitted.

I think it's a good idea to catch a PlatformException and branch the process with code as shown below.

try {
  final result = await _auth.createUserWithEmailAndPassword(
      email: email,
      password: password,
  );
} on PlatformException catch (exception) {
  switch (exception.code) {
  case 'ERROR_EMAIL_ALREADY_IN_USE':
    // do something...
  default:
    break;
}
Sign up to request clarification or add additional context in comments.

Comments

0

use on PlatformException catch (e) and if (e.message == 'ERROR_EMAIL_ALREADY_IN_USE') to handle this case.

Comments

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.