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();
});
}
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With