【问题标题】:Firebase Flutter, unable to catch ExceptionFirebase Flutter,无法捕获异常
【发布时间】:2021-04-24 00:58:51
【问题描述】:

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

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

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

【问题讨论】:

    标签: firebase flutter firebase-authentication


    【解决方案1】:

    为什么要抓PlatformException?此方法的文档中提到了FirebaseAuthException,请尝试了解:

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

    【讨论】:

    • 我进行了请求的更改,但错误仍然存​​在。请查看我发布的更新 1。谢谢。
    • 这是一个抛出的异常,而不是错误。您在日志中看到任何错误吗?
    • 哦。日志中没有错误,但我想打印消息“异常捕获!”这也没有显示在日志中。抛出此异常后,我的程序卡住了。我怎样才能防止它冻结?
    猜你喜欢
    • 1970-01-01
    • 2017-08-10
    • 2021-04-25
    • 2021-09-02
    • 2020-08-13
    • 2020-06-17
    • 1970-01-01
    • 2017-08-18
    • 2010-10-29
    相关资源
    最近更新 更多