here is my code, what i want to achieve is to show a snackbar to a user with the error which works fine but then the error is not caught and the app gets hanged ...
void signUp(String email, String password) async {
if (_formKey.currentState!.validate()) {
try {
await _auth
.createUserWithEmailAndPassword(email: email, password: password)
.then((value) => {postDetailsToFirestore()})
.catchError((e) {
Fluttertoast.showToast(msg: e!.message);
});
} on FirebaseAuthException catch (error) {
switch (error.code) {
case "invalid-email":
errorMessage = "Your email address appears to be malformed.";
break;
case "wrong-password":
errorMessage = "Your password is wrong.";
break;
case "user-not-found":
errorMessage = "User with this email doesn't exist.";
break;
case "user-disabled":
errorMessage = "User with this email has been disabled.";
break;
case "too-many-requests":
errorMessage = "Too many requests";
break;
case "operation-not-allowed":
errorMessage = "Signing in with Email and Password is not enabled.";
break;
default:
errorMessage = "An undefined Error happened.";
}
// default snackbar
Fluttertoast.showToast(msg: errorMessage!);
print(error.code);
// custom snackbar
ScaffoldMessenger.of(context)
..hideCurrentSnackBar()
..showSnackBar(getSnack( errorMessage!, ContentType.failure, 'Failed !'));
}
}
}
now the problem is that it works fine when a new user creates an account but this error don't get caught when the user registers with the same email twice ... tried a lot of things like returning future and formatting and on error instead of catch error but i'm stuck on it for hours ...
CodePudding user response:
The case that you need to check is email-already-in-use
and it should catch it when registering for a user using createUserWithEmailAndPassword
method. You can check for other error codes in the documentation.
Also, if you're using async/await
in your code, it's unnecessary to use then().catchError()
in this case. You are fine just doing
try {
await _auth.createUserWithEmailAndPassword(email: email, password: password);
postDetailsToFirestore();
} on FirebaseAuthException catch (error) {
// handle error
}