Reputation: 395
I'm trying to making a signin feature on my app using firebase auth on flutter.
There are many exceptions I have to deal with but I can't catch them at all.
Here is my code.
Future<void> onSubmitted(
String email, AnimationController controller, BuildContext context) async {
controller.repeat();
FirebaseAuth _auth = FirebaseAuth.instance;
try {
_auth.signInWithEmailAndPassword(email: email, password: "1");
} on PlatformException catch (e) {
debugPrint(e.toString());
} on FirebaseAuthException catch (e) {
debugPrint(e.toString());
}
}
This is simplified form for testing. Anyway it doesn't even reach the catch statement. It raises PlatformException before it reaches the catch statement. I don't understand.
The error code is like this.
Unhandled Exception: [firebase_auth/user-not-found] There is no user record corresponding to this identifier. The user may have been deleted.
I explicitly specified the type of the exception and it's supposed to print the error message but it doesn't even reach debugPrint()
function.
How should I catch the exception?
Upvotes: 3
Views: 4197
Reputation: 6165
This happens because the method signInWithEmailAndPassword is asynchronous, thus you need to await the result for the catch to work, like this:
try {
await _auth.signInWithEmailAndPassword(email: email, password: "1");
} on PlatformException catch (e) {
debugPrint(e.toString());
} on FirebaseAuthException catch (e) {
debugPrint(e.toString());
}
Upvotes: 0
Reputation: 564
This appears to be an issue with how errors are thrown and caught when dealing with async, FlutterFire hasn't gotten around to putting theirs into try/catch yet - which we can't do anything about.
Here they discuss it: https://github.com/FirebaseExtended/flutterfire/issues/3475
Here is a pending fix: https://github.com/FirebaseExtended/flutterfire/pull/3700
At the moment the answer seems to be essentially to either step through the exceptions, or uncheck 'Uncaught exceptions' in the debugger (which is not great since it won't catch actual exceptions..)
Upvotes: 0
Reputation: 609
According to the official FlutterFire Documentation, you should store the `UserCredential`
retrieved from method `signInWithEmailAndPassword()` object in a variable. So your code should look something like this:
try {
UserCredential userCredential = await FirebaseAuth.instance.signInWithEmailAndPassword(
email: email,
password: "1"
);
} on FirebaseAuthException catch (e) {
if (e.code == 'user-not-found') {
print('No user exists with this email.');
} else if (e.code == 'wrong-password') {
print('Incorrect Password.');
}
}
Upvotes: 1
Reputation: 395
The problem was 'they were actually caught'.
There is no problem in catch statement or anywhere but the behavior of the debugger was the problem. On Android Studio I can adjust the setting so it doesn't stop when the exception raises but on VSCode, no matter what I do, it will stop when the exception is raised before the catch statement.
So if I want to continue to the next lines, I just have to click continue in the debugging toolbar.
But I really hope I can change the setting of the breaks on exceptions on VSCode as well.
Upvotes: 4
Reputation: 1033
Have you done all the necessary?
Also, _auth.signInWithEmailAndPassword(email: email, password: "1") is a async process and takes time to complete.
You need to have await
Upvotes: 0