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

喜你入骨 提交于 2020-01-11 12:22:37

问题


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'

Here's my Code:

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.


回答1:


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

AuthResult userDetails = await _firebaseAuth.signInWithCredential(credential);

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



来源:https://stackoverflow.com/questions/59453599/a-value-of-type-authresult-cant-be-assigned-to-a-variable-of-type-firebaseus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!