1

I'm trying to get my head around this problem. Problem is no matter what I do, I'm unable to catch the exception for my application. The program instead of printing the error just gets stuck (due to uncaught exception).

This is my simple code. Can someone please help me in solving this problem?

Future<void> onRegisterPress(
    BuildContext context,
    TextEditingController fName,
    TextEditingController lName,
    TextEditingController email,
    TextEditingController password) async {
  if (isValid(fName.text, lName.text, email.text, password.text)) {
    //TODO: Exception not caught
    try {
      var userCredential = await FirebaseAuth.instance
          .createUserWithEmailAndPassword(
              email: email.text, password: password.text);
    } on PlatformException catch (e) {
      print('Failed with error code: ${e.code}');
      print(e.message);
      Navigator.push(
          context,
          MaterialPageRoute(
              builder: (context) => MyHomePage(headline: 'Exception Caught!')));
    }

    Navigator.push(
        context, MaterialPageRoute(builder: (context) => MyLoginPage()));
  }
}

Update 1:

User user = FirebaseAuth.instance.currentUser;
    try {
      await FirebaseAuth.instance.createUserWithEmailAndPassword(
          email: email.text, password: password.text);
    } on FirebaseAuthException catch (e) {
      print('Failed with error code: ${e.code}');
      print(e.message);
      Navigator.push(
          context,
          MaterialPageRoute(
              builder: (context) => MyHomePage(headline: 'Exception Caught!')));
    }

Updating the code using FirebaseAuthException, has no effect unfortunately.

Error is: Error Screenshot

1 Answer 1

1

Why do you want to catch PlatformException? Documentation for this method mentions FirebaseAuthException, try to catch that:

try {
  await FirebaseAuth.instance.createUserWithEmailAndPassword(...)
} on FirebaseAuthException catch(e) {
  ...
}
Sign up to request clarification or add additional context in comments.

3 Comments

I made the requested changes but the error persists. Kindly have a look at update 1 I posted. Thanks.
it's a thrown exception, not an error. do you see any error in logs?
Oh. There is no error in the logs but I want to print the message "Exception Caught!" which is not displaying in the logs either. After throwing this exception my program became stuck. How can I prevent it from freezing?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.