一尘不染

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

flutter

我正在使用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;
}

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


阅读 320

收藏
2020-08-13

共1个答案

一尘不染

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

AuthResult userDetails = await _firebaseAuth.signInWithCredential(credential);

对于您的代码,另一种选择也是更好的选择,因为signInWithCredential返回AuthResult并且由于class
AuthResult包含usertype的实例变量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

2020-08-13