无法将类型“ AuthResult”的值分配给“ FirebaseUser”类型的变量

时间:2019-12-23 10:21:04

标签: firebase flutter dart firebase-authentication

我正在使用Firebase Google登录选项在我的项目中实现google登录方法,当在我的代码中添加以下行时,它会向我抛出如下错误:

A value of type 'AuthResult' can't be assigned to a variable of type 'FirebaseUser'

这是我的代码:

final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
final GoogleSignIn _googlSignIn = new GoogleSignIn();

Future<FirebaseUser> _signIn(BuildContext context) async {

  Scaffold.of(context).showSnackBar(new SnackBar(
    content: new Text('Sign in'),
  ));

  final GoogleSignInAccount googleUser = await _googlSignIn.signIn();
  final GoogleSignInAuthentication googleAuth =await googleUser.authentication;

  final AuthCredential credential = GoogleAuthProvider.getCredential(
    accessToken: googleAuth.accessToken,
    idToken: googleAuth.idToken,
  );

  FirebaseUser userDetails = await _firebaseAuth.signInWithCredential(credential).user;
  ProviderDetails providerInfo = new ProviderDetails(userDetails.providerId);

  List<ProviderDetails> providerData = new List<ProviderDetails>();
  providerData.add(providerInfo);

  UserDetails details = new UserDetails(
    userDetails.providerId,
    userDetails.displayName,
    userDetails.photoUrl,
    userDetails.email,
    providerData,
  );

  Navigator.push(
    context,
    new MaterialPageRoute(
      builder: (context) => new Profile(detailsUser: details),
    ),
  );
  return userDetails;
}

有人可以告诉我是什么问题。

2 个答案:

答案 0 :(得分:2)

方法firebaseAuth.signInWithCredential(credential)返回类型为AuthResult的值,因此您需要执行以下操作:

AuthResult userDetails = await _firebaseAuth.signInWithCredential(credential);

另一种更好的选择是因为signInWithCredential返回AuthResult,并且由于类AuthResult包含类型为user的实例变量FirebaseUser ,那么您可以执行以下操作:

FirebaseUser userDetails = (await _firebaseAuth.signInWithCredential(credential)).user;

https://github.com/FirebaseExtended/flutterfire/blob/master/packages/firebase_auth/firebase_auth/lib/src/auth_result.dart#L18

答案 1 :(得分:0)

简单的解决方案(由类型更改引起):

Firebase最近将其类型“ FirebaseUser”(现已折旧)类型更改为“ User”。尝试在代码中重命名,这应该是解决方案。就我而言,它可以解决大多数相同的错误。

最佳