0

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?

RTXGamer
  • 3,215
  • 6
  • 20
  • 29
r0czak
  • 1
  • 1
    The caller doesn't catch the rethrown error because it doesn't `await` the returned `Future`. – jamesdlin Nov 09 '21 at 04:43
  • Thank you so much, this solved my problem immediately! I'm new with Stack mechanics, can I highlight your answer, so others will know the answer? @jamesdlin – r0czak Nov 09 '21 at 15:38

0 Answers0