我如何处理 google_sign_in 上的错误 NoSuchMethodError: The getter 'authentication' was called on null?

时间:2021-01-03 04:15:18

标签: flutter firebase-authentication

在我的模拟器中,当我点击使用 Google 登录时,它产生了这个错误

NoSuchMethodError: The getter 'authentication' was called on null.

我能够通过在模拟器中退出谷歌帐户并再次登录来解决错误。

此问题是否特定于模拟器的帐户?如果我使用网络术语,这似乎是某种类型的会话到期。

我想知道这是否会在真实设备上发生。

2 个答案:

答案 0 :(得分:0)

您似乎尚未在 android 部分添加身份验证证书。

或者您遗漏了一些 android 的依赖项和导入语句。

我也遇到了同样的问题,但可以通过这种方法解决。

使用这个https://firebase.flutter.dev/docs/auth/social/ 因为其他方法有点棘手。

Future<UserCredential> signInWithGoogle() async {
  // Trigger the authentication flow
  final GoogleSignInAccount googleUser = await GoogleSignIn().signIn();

  // Obtain the auth details from the request
  final GoogleSignInAuthentication googleAuth = await googleUser.authentication;

  // Create a new credential
  final GoogleAuthCredential credential = GoogleAuthProvider.credential(
    accessToken: googleAuth.accessToken,
    idToken: googleAuth.idToken,
  );

  // Once signed in, return the UserCredential
  return await FirebaseAuth.instance.signInWithCredential(credential);
}```

答案 1 :(得分:0)

对于我这样调用函数

"AuthService(FirebaseAuth.instance).signInWithGoogle();" 

代替

"AuthService(auth).signInWithGoogle();" 

修复了这个错误。 但我是在手机上而不是模拟器上测试的。