我正在努力使我的应用程序的web版本在flutter中可用。在android和ios的移动版中,使用谷歌凭据登录非常有效。在web flutter中,我正确地获得了idToken,但accessToken为空。
我遵循了此视频中的步骤:https://www.youtube.com/watch?v=0HLt1TYA600&list=LLe08IbNO0_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上打开了一个问题,得到了一个答案:
嘿,请更新到firebase_auth:^0.18.0+1-断言中有一个错误已修复。最新版本可在此处查看:https://firebase.flutter.dev/docs/migration/#2-更新firebase插件
要澄清的是,问题不在于accessToken
为null,而是它不必为非null。错误是有一个断言来检查idToken
或accessToken
是否不为null,但实际上它检查两者都不为null。
为了完整起见,以下是修复此错误的确切行更改。请注意它们是如何从检查accessToken != null && idToken != null
更改为accessToken != null || idToken != null
的。