Firebase Flutter,无法捕获异常

时间:2021-01-19 17:19:52

标签: firebase flutter firebase-authentication

我正在努力解决这个问题。问题是无论我做什么,我都无法捕捉到我的应用程序的异常。程序只是卡住了而不是打印错误(由于未捕获的异常)。

这是我的简单代码。有人可以帮我解决这个问题吗?

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()));
  }
}

更新 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!')));
    }

不幸的是,使用 FirebaseAuthException 更新代码没有效果。

错误是: Error Screenshot

1 个答案:

答案 0 :(得分:0)

你为什么要抓住PlatformException?此方法的文档中提到了 FirebaseAuthException,请尝试抓住这一点:

try {
  await FirebaseAuth.instance.createUserWithEmailAndPassword(...)
} on FirebaseAuthException catch(e) {
  ...
}