无法将类型“ AuthResult”的值分配给“ FirebaseUser”类型的变量 [英] A value of type 'AuthResult' can't be assigned to a variable of type 'FirebaseUser'

查看:108
本文介绍了无法将类型“ 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

这篇关于无法将类型“ AuthResult”的值分配给“ FirebaseUser”类型的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆