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

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

问题描述:

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

I'm implementing the google sign in method in my project using Firebase Google sign in option, when the add the below line in my code its throwing me the error like:

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;
}

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

Can someone tell me whats the problem please.

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

The method firebaseAuth.signInWithCredential(credential) returns a value of type AuthResult, therefore you need to do the following :

AuthResult userDetails = await _firebaseAuth.signInWithCredential(credential);

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

The other alternative and the better one for your code, is since signInWithCredential returns AuthResult and since class AuthResult contains instance variable user of type FirebaseUser, then you can do the following:

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