简体   繁体   English

Flutter 在尝试使用 googleSingIn 进行 Firebase 身份验证时无法捕获 PlatformException

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

I have just started using flutter, I am trying to implement firebase signing in using googleAuth and it shows these errors below:我刚刚开始使用 flutter,我正在尝试使用 googleAuth 实现 firebase 登录,它在下面显示了这些错误:

在此处输入图片说明

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!谢谢,我希望这会有所帮助!

You can do trick by use Future Object你可以通过使用 Future Object 来做技巧

by example i want to use PhoneAuth and check Code write by user is true or not so the problem is cant catch the the exception come after user put wrong code例如,我想使用 PhoneAuth 并检查用户编写的代码是否正确,因此问题是无法捕获用户输入错误代码后出现的异常

try this code first declare the async method like this试试这段代码,首先像这样声明异步方法

Future<FirebaseUser> _signInWithCode(String code) async {
    AuthCredential phoneAuthCredential = PhoneAuthProvider.getCredential(
        verificationId: verificationID, smsCode: code);
    try {
      AuthResult result = await mAuth.signInWithCredential(phoneAuthCredential);

      FirebaseUser currentUser = await mAuth.currentUser();
      if (currentUser != null && result.user.uid == currentUser.uid) {
        return currentUser;
      } else {
        return null;
      }
    } on PlatformException catch (exp) {}

    return null;
  }

so if exp thrown the value come is null所以如果 exp 抛出,则值 come 为 null

and some where in my code after user submit code use this code用户提交代码后我的代码中的一些地方使用此代码

FirebaseUser user = await _signInWithCode(code);

if (user != null)
      debugPrint("true input code");
    else {
      debugPrint("WRONNNG input code");
      setState(() {
        isValidCode = false;
      });
    }

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

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