使用Flutter进行Google登录:错误代码-4 [英] Google Sign-In With Flutter: Error Code -4

查看:134
本文介绍了使用Flutter进行Google登录:错误代码-4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试在Flutter中实现google_sign_in软件包( https://pub.dartlang.org/packages/google_sign_in ).

I currently try to implement google_sign_in package in Flutter (https://pub.dartlang.org/packages/google_sign_in).

为此,我遵循了其存储库的示例( https://github.com/flutter/plugins/blob/master/packages/google_sign_in/lib/google_sign_in.dart ).

For this, I followed the example of their repository (https://github.com/flutter/plugins/blob/master/packages/google_sign_in/lib/google_sign_in.dart).

在该示例中,"initState"中的调用是 signInSilently .

In that example in "initState" is a call signInSilently.

@override
void initState() {
  super.initState();
  _googleSignIn.onCurrentUserChanged.listen((GoogleSignInAccount account) {
    setState(() {
      _currentUser = account;
      loggedIn = true;
    });
  });
  loggedIn = false;
  _googleSignIn.signInSilently();
}

我在iOS中尝试了此代码.在我的第一个App Start上,它运行良好.但是自从我注销后,我一直在重新启动应用程序时一直在这里遇到错误.它是以下PlatformException:

I tried this code in iOS. On my first App Start, it worked well. But since I logged out I get an error here all the time I restart my app.It is the following PlatformException:

PlatformException(sign_in_required, com.google.GIDSignIn, The operation couldn’t be completed. (com.google.GIDSignIn error -4.))

我在问题 Google登录错误-4 中发现该错误代码是由于钥匙串中缺少Auth.

I found in question Google Sign-In Error -4 that the error code is because of a missing Auth in Keychain.

快速编程时的解决方案是在尝试SignInSilently之前调用方法hasAuthInKeychain *.我的问题是flutter包中的 GoogleSignIn 类没有这样的函数.

The solution while swift programming is to call the method * hasAuthInKeychain* before the try to signInSilently. My problem is that the GoogleSignIn class in the flutter package has no function named like this.

是否需要使用此软件包运行另一个电话以确保我可以尝试静默登录?还是我做错了什么以获取此消息,或者甚至有可能捕获到此错误?

Is there another call I need to run with this package to be sure I can try a silent log in? Or am I doing something wrong to get this message or is there even the possibility of catching this error?

我也尝试过Marcel的解决方案.不知何故,它没有捕获PlatfromException.

I tried Marcel's solution, too. Somehow it is not catching the PlatfromException.

我不知道这是否有帮助:signInSilently()正在调用一个方法,该方法中存在以下调用(google_sign_in.dart,第217行):

I do not know if this will help: signInSilently() is calling a method in which there is a the following call (google_sign_in.dart, line 217):

await channel.invokeMethod(method)

platform_channel.dart中有一个呼叫

In platform_channel.dart there is a call

codec.decodeEnvelope(result);

这里会抛出平台异常.

if (errorCode is String && (errorMessage == null || errorMessage is String) && !buffer.hasRemaining)
  throw PlatformException(code: errorCode, message: errorMessage, details: errorDetails);
else
  throw const FormatException('Invalid envelope');

编辑2

由于我只是运行我的应用程序而未在调试模式下启动它,因此它在某种程度上又可以正常工作而不会引发异常.我不知道这如何影响代码以及为什么会出现此异常.我也可以再次在调试模式下运行代码.

Edit 2

Since I just run my app and not started it in debug mode it somehow works again without throwing an exception. I do not know how this affects the code and why I got this exception. I can also run the code in debug mode again.

从那时起,我又一次遇到了例外.再次,我重新启动了android studio,并在没有调试模式的情况下运行了该应用程序一次.

Since then I had the exception once again. Again I restarted android studio and runned the application once without debug mode.

推荐答案

就我而言,我不希望用户自动看到登录窗口.在这种情况下,我从 signIn 更改为 signOut .这样,我将用户发送到另一个视图,并附带一条解释性消息和一个登录按钮.

In my case, I did not want the user to see the login window automatically. In this case I changed from signIn to signOut. This way, I send the user to another view with an explanatory message and a login button.

GoogleSignInAccount googleSignInAccount = await googleSignIn
    .signInSilently(suppressErrors: false)
    .catchError((dynamic error) async {
      GoogleSignInAccount googleSignInAccount = await _googleSignIn.signOut();
      return googleSignInAccount;
    });

这篇关于使用Flutter进行Google登录:错误代码-4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆