I'm currently testing my app, when I try to register a new account with an email already it in use, I get the following error:
ArgumentError (Invalid argument(s) (onError): The error handler of Future.catchError must return a value of the future's type)
When I click sign in the app still informs the user that this email is already in use but the error crashes the app. If I try to test any of the other errors, my app does not crash.
I'm not quite sure how to begin to resolve this due to not really being sure why is error is occurring.
*.dart
void signUp(String email, String password) async {
if (GlobalKey<FormState>().currentState!.validate()) {
try {
await FirebaseAuth.instance //<--- Error stops here
.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 incorrect.";
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.";
}
Fluttertoast.showToast(msg: errorMessage!);
print(error.code);
}
}
}
postDetailsToFirestore() async {
FirebaseFirestore firebaseFirestore = FirebaseFirestore.instance;
User? user = _auth.currentUser;
UserModel userModel = UserModel();
// writing all the values
userModel.email = user!.email;
userModel.uid = user.uid;
userModel.userName = NameEditingController.text;
userModel.password = passwordEditingController.text;
await firebaseFirestore
.collection("users")
.doc(user.uid)
.set(userModel.toMap());
Fluttertoast.showToast(msg: "Account created successfully ");
Navigator.pushAndRemoveUntil(
(context),
MaterialPageRoute(builder: (context) => HomeScreen()),
(route) => false);
}
CodePudding user response:
async/await is just a syntactic sugar to handle promises. It makes your code more readable, and combining it with try/catch you can have a more clear way of handling exceptions.
I also recommend to handle the situation if the mobile device does not have Internet access, and a SocketException is thrown. For this kind of exception you need import 'dart:io';
Don't forget to add a simple catch as well, because you can have exceptions other than FirebaseAuthException and SocketException, and these will be not caught if you use only on.(You don't need the catch after on.)
Try it this way:
try {
await FirebaseAuth.instance.createUserWithEmailAndPassword(email: email,
password: password);
await postDetailsToFirestore();
} on FirebaseAuthException (error) {
...
} on SocketException {
...
} catch (e) {
..
}
