如何解决Web flutter中GoogleSignInAuthentication accessToken的accessToken为null
我正在尝试使我的应用程序的网络版本不可用。在Android和ios的移动版本中,使用Google凭据登录均能完美运行。在网络抖动中,我正确地获得了idToken,但accessToken为null。
我按照此视频中的步骤操作:https://www.youtube.com/watch?v=0HLt1TYA600&list=LLe08PibNO0_PFjUVIu5WcxQ&index=3&t=1681s
Future<User> signInWithGoogle() async {
final googleSignIn = GoogleSignIn();
try {
final GoogleSignInAccount googleSignInAccount =
await googleSignIn.signIn();
if (googleSignInAccount != null) {
final GoogleSignInAuthentication googleSignInAuthentication =
await googleSignInAccount.authentication;
print(">>> accessToken");
print(googleSignInAuthentication.accessToken);
print(">>> idToken");
print(googleSignInAuthentication.idToken);
if (googleSignInAuthentication.accessToken != null &&
googleSignInAuthentication.idToken != null) {
final AuthCredential credential = GoogleAuthProvider.getCredential(
idToken: googleSignInAuthentication.idToken,accessToken: googleSignInAuthentication.accessToken,);
final AuthResult authResult =
await FirebaseAuth.instance.signInWithCredential(credential);
final FirebaseUser user = authResult.user;
return _userFromFirebase(user);
} else {
throw PlatformException(
code: 'ERROR_MISSING_GOOGLE_AUTH_TOKEN',message: 'Missing Google Auth Token',);
}
} else {
throw PlatformException(
code: 'ERROR_ABORTED_BY_USER',message: 'Sign in aborted by user',);
}
} catch (error) {
return null;
}
}
解决方法
昨晚我在GitHub上打开了issue,并得到了答案:
嘿,请更新为firebase_auth:^ 0.18.0 + 1-断言中的一个错误已修复。 最新版本可在此处查看:https://firebase.flutter.dev/docs/migration/#2-update-firebase-plugins
为澄清起见,问题不在于accessToken
为空,而是它不必非为null。错误是存在一个断言来检查 idToken
或accessToken
是否不为空,但实际上它会检查两者都不为空
为了完整起见,here是修复此错误的确切行更改。请注意,它们是如何从检查accessToken != null && idToken != null
更改为accessToken != null || idToken != null
的。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。