Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception Firebase Auth on flutter, using vccode

Tags:

flutter

dart

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

  }
like image 260
2kBIT Avatar asked Aug 06 '26 23:08

2kBIT


1 Answers

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;
}
like image 118
Muramatsu Ryunosuke Avatar answered Aug 12 '26 07:08

Muramatsu Ryunosuke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!