Try catch block doesn't catch rethrown error

85 views Asked by At

UI code:

try {
    authService.signInWithEmailAndPassword(
        emailController.text, passwordController.text);
} catch (error) {
    print("ui rethrow");
}

Auth service code:

Future<User?> signInWithEmailAndPassword(
    String email,
    String password,) async {
        try {
            final credential = await _firebaseAuth.signInWithEmailAndPassword(
                email: email, password: password);
            return _userFromFirebase(credential.user);
        } catch (e) {
            print("service throw");
            rethrow;
        }
    }

I want to rethrow FirebaseAuthException from the authentication service to the UI, so I can give a user prompt with what went wrong, but the UI try-catch block doesn't catch the rethrown error.

Why doesn't my code work?

0

There are 0 answers