“ FirebaseUser”未来的问题

时间:2020-09-14 01:53:38

标签: firebase flutter firebase-authentication google-signin

我在一个新项目中使用Firebase实施Google登录时遇到问题,说该表达式不能评估函数。在过去的一年中,该代码已在我的其他项目中使用。这是我的代码:

class FirebaseAuthService {
  final FirebaseAuth _firebaseAuth;
  final GoogleSignIn _googleSignIn;

  FirebaseAuthService({FirebaseAuth firebaseAuth, GoogleSignIn googleSignin})
      : _firebaseAuth = firebaseAuth ?? FirebaseAuth.instance,
        _googleSignIn = googleSignin ?? GoogleSignIn();

  Account _userFromFirebase(User user) {
    if (user == null) {
      return null;
    }
    return Account(
      uid: user.uid,
      email: user.email,
      displayName: user.displayName,
      photoUrl: user.photoURL,
    );
  }

  Stream<Account> get onAuthStateChanged {
    return _firebaseAuth.onAuthStateChanged.map(_userFromFirebase);
  }

  Future<Account> signInAnonymously() async {
    final authResult = await _firebaseAuth.signInAnonymously();
    return _userFromFirebase(authResult.user);
  }

  Future<Account> signInWithGoogle() async {
    final googleUser = await _googleSignIn.signIn();
    final googleAuth = await googleUser.authentication;
    final credential = GoogleAuthProvider.credential(
      accessToken: googleAuth.accessToken,
      idToken: googleAuth.idToken,
    );
    final authResult = await _firebaseAuth.signInWithCredential(credential);
    return _userFromFirebase(authResult.user);
  }

  Future<void> signOut() async {
    return _firebaseAuth.signOut();
  }

  Future<Account> currentUser() async {// The Issue is here in the Future<>
    final user = await _firebaseAuth.currentUser();
    return _userFromFirebase(user);
  }
}

1 个答案:

答案 0 :(得分:1)

在最新版本的firebase_auth(0.18.0 + 1)中,没有currentUser函数,并且它不返回Future。许多FlutterFire软件包都进行了重大的重大突破更改,这些更改可能已更改了此语法,您可能没有查看这些重大更改的变更日志就更新了软件包。

获取currentUser现已更改为吸气剂,不再返回Future

currentUser函数应修改为以下内容:

Account currentUser() {
  final user = _firebaseAuth.currentUser;
  return _userFromFirebase(user);
}