简体   繁体   English

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

[英]Flutter unable to catch PlatformException while attempting firebase auth

I just recently began using flutter, I am trying to implement firebase signing in using email and password and it works when I input the correct email and password but not when the password is wrong.我最近才开始使用 flutter,我正在尝试使用电子邮件和密码实现 firebase 登录,当我输入正确的电子邮件和密码时它可以工作,但在密码错误时却不起作用。 It gives a PlatformException when the password is wrong and then the app freezes even though I've tried to catch the exception while android studio shows this below:当密码错误时,它会给出 PlatformException ,然后应用程序冻结,即使我试图在 android studio 显示以下内容时捕获异常:

在此处输入图片说明

Here is my sign in implementation:这是我的登录实现:

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

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

Please let me know how to catch the exception, thank you.请告诉我如何捕获异常,谢谢。

I was having the same issue for a while and found out the reason why.我有一段时间遇到了同样的问题,并找出了原因。

The issue here is, although it "seems" you cannot catch the PlatformException , in reality, you "are catching" the exception.这里的问题是,尽管“似乎”您无法捕获 PlatformException ,但实际上,您“正在捕获”异常。

In Android Studio, we can set the debug breaking policy, by going to Run > View Breakpoints.在 Android Studio 中,我们可以通过运行 > 查看断点来设置调试中断策略。 在此处输入图片说明

If you uncheck the Enabled, you will find out that the Android Studio "doesn't get in the way" of your catching the exception and your app will display the snackbar properly when catch clause executes which is what we expect "when we are not debugging" the app.如果您取消选中 Enabled,您会发现 Android Studio“不会妨碍”您捕获异常,并且您的应用程序将在执行 catch 子句时正确显示小吃栏,这正是我们所期望的“当我们没有调试”应用程序。

But when we are debugging with the Break on exceptions enabled, Android Studio simply does its job by breaking on PlatformException before the catch clause executes.但是当我们在启用 Break on exceptions 的情况下进行调试时,Android Studio 只是通过在 catch 子句执行之前中断 PlatformException 来完成它的工作。

So, you could just uncheck the Enabled option or just press the "Resume Program" button in Android Studio and just not worry about it.因此,您可以取消选中“已启用”选项,或者只需按下 Android Studio 中的“恢复程序”按钮,而不必担心。 You're app is probably working just fine.您的应用程序可能运行良好。 :) :)

I have also recently faced this error and, I have found out that the .catchError() callback wasn't being called in the debug mode (which is when you click the Run->Start Debugging button in VSCode).我最近也遇到了这个错误,我发现.catchError()回调没有在调试模式下被调用(当你在 VSCode 中点击Run->Start Debugging按钮时)。

However, when you type in flutter run -d , then, the .catchError() method gets called back as it is not in debug mode.但是,当您输入 flutter run -d 时, .catchError()方法会被回调,因为它不在调试模式下。

To get your preferred simulator's code paste this line of code in the terminal:要获得首选模拟器的代码,请在终端中粘贴以下代码行:

instruments -s devices

If that doesn't work, you can also try pasting this:如果这不起作用,您也可以尝试粘贴以下内容:

xcrun simctl list

The .catchError() method will get called unlike before and the code inside that will get executed as expected! .catchError()方法将像以前一样被调用,并且其中的代码将按预期执行!

Additionally, the app won't crash anymore with a PlatformException() and instead you will get a log like this one:此外,应用程序不会再因PlatformException()而崩溃,而是您将获得如下日志:

[VERBOSE-2:ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: The getter 'uid' was called on null.
Receiver: null

I have been facing this problem in Google Sign In too, in which the .catchError() was not being called!我在 Google Sign In 中也.catchError()过这个问题,其中.catchError()没有被调用!

In conclusion, if you have some error with handling errors in Firebase Authentication, you should first try to first run in through the terminal.总之,如果您在处理 Firebase 身份验证中的错误时遇到一些错误,您应该首先尝试首先通过终端运行。 Thanks, and I hope this helps!谢谢,我希望这会有所帮助!

使用 FirebaseException 而不是 PlatformException

on FirebaseException catch (e){}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM