user4282023
user4282023

Reputation:

Firebase Flutter, unable to catch Exception

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

Upvotes: 1

Views: 80

Answers (1)

mfkw1
mfkw1

Reputation: 1151

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) {
  ...
}

Upvotes: 1

Related Questions