Flutter在尝试Firebase身份验证时无法捕获PlatformException

Rob*_*isa 5 android exception firebase firebase-authentication flutter

我最近才开始使用flutter,我正在尝试使用电子邮件和密码来实现Firebase登录,当我输入正确的电子邮件和密码时,它可以工作,但是当密码错误时,它不能使用。当密码错误时,它将给出一个PlatformException,然后应用程序冻结,即使我在android studio显示以下情况时试图捕获该异常:

在此处输入图片说明

这是我的实施登录:

String _reason;
  Future<void> _performLogin() async {
    String reason;
    // This is just a demo, so no actual login here.
    try{
      FirebaseUser user =  await FirebaseAuth.instance.signInWithEmailAndPassword(email: _email, password: _password);
      if(user != null){
        Navigator.of(context).push(new MaterialPageRoute<dynamic>(
          builder: (BuildContext context) {
            return new MyApp();
          },
        ));
        final snack = new SnackBar(
          content: new Text("Signed In Successfully"),
          action: null,
          duration: new Duration(seconds: 4),
          backgroundColor: Colors.black,
        );

        Scaffold.of(context).showSnackBar(snack);
      }
    }on PlatformException catch(e){

      reason = '${e.message}';

      /*final snack = new SnackBar(
        content: new Text(e.message),
        action: null,
        duration: new Duration(seconds: 4),
        backgroundColor: Colors.black,
      );

      Scaffold.of(context).showSnackBar(snack);*/
    }
    setState(() {
      _reason = reason;
    });

  }
Run Code Online (Sandbox Code Playgroud)

我还加了 import 'package:flutter/services.dart' show PlatformException;

请让我知道如何捕获异常,谢谢。

iec*_*fly 5

我有一段时间遇到了同样的问题,并找出了原因。

这里的问题是,尽管“似乎”您无法捕获 PlatformException ,但实际上,您“正在捕获”异常。

在 Android Studio 中,我们可以通过运行 > 查看断点来设置调试中断策略。 在此处输入图片说明

如果您取消选中已启用,您会发现 Android Studio“不会妨碍”您捕获异常,并且您的应用程序将在执行 catch 子句时正确显示小吃栏,这正是我们所期望的“当我们不是调试”应用程序。

但是当我们在启用 Break on exceptions 的情况下进行调试时,Android Studio 只是通过在 catch 子句执行之前中断 PlatformException 来完成它的工作。

因此,您可以取消选中“已启用”选项,或者只需按下 Android Studio 中的“恢复程序”按钮,而不必担心。您的应用程序可能运行良好。:)


Rah*_*ram -8

在 firebase 控制台设置中更新您的 android debug sha1certificate 指纹并尝试