Firebase.instance.signOut() 未将 currentUser() 设置为 null

Posted

技术标签:

【中文标题】Firebase.instance.signOut() 未将 currentUser() 设置为 null【英文标题】:Firebase.instance.signOut() not setting the currentUser() to null 【发布时间】:2020-05-04 02:36:34 【问题描述】:

为什么我的 signOut() 方法没有将 currentUser() 设置为 null?

当我在主页上单击注销时,它会检查 isAuthenticated() 方法,该方法返回的是用户而不是 null。另外,看看我在 logout() 方法中包含的两个 cmets。 谢谢!!

这是我的代码 -

class Auth with ChangeNotifier 
  final FirebaseAuth _auth = FirebaseAuth.instance;
  final GoogleSignIn _googleSignIn = GoogleSignIn();

  bool isAuthenticated() 
    print('$currentUser inside the isAuthenticated');
    if (currentUser == null) 
      print('false');
      return false;
    
    print('true');
    return true;
  

  Future<FirebaseUser> get currentUser async 
    return await _auth.currentUser();
  


  Future<FirebaseUser> signInWithEmail(String email, String password) async 
    try 
      final response = await _auth.signInWithEmailAndPassword(
        email: email,
        password: password,
      );
      print(response);

      if (response == null) 
        print('response is null');
       else 
        notifyListeners();
        return response.user;
      
     catch (error) 
      print('some error');
      throw error;
    
  

  Future<FirebaseUser> signInWithGoogle() async 
    try 
      final GoogleSignInAccount _userDetails = await _googleSignIn.signIn();
      if (_userDetails == null) 
        print('No user found');
       else 
        final GoogleSignInAuthentication googleAuth =
            await _userDetails.authentication;

        final AuthCredential credential = GoogleAuthProvider.getCredential(
          accessToken: googleAuth.accessToken,
          idToken: googleAuth.idToken,
        );

        final FirebaseUser user =
            (await _auth.signInWithCredential(credential)).user;

        return user;
      
      notifyListeners();
      print('google sign in ran successfully');
     catch (error) 
      throw error;
    
  

  Future<FirebaseUser> logout() async 
    try 
      print('logout ran');
      print(currentUser);       //THIS RETURNS AN INSTANCE OF USER
      _auth.signOut().then( (response) 
        print('signout successful');
      );

      await _googleSignIn.signOut();
      print(currentUser);     //THIS *ALSO* RETURNS AN INSTANCE OF USER
      notifyListeners();
      return currentUser;
     catch (error) 
      print('an error');
    
  

我知道会有一个我无法抓住的愚蠢错误,但现在很累!!我尝试了其他可以在 *** 上找到的方法

【问题讨论】:

【参考方案1】:

当您尝试获取当前用户时,可能是未来不完整,您是否尝试过等待而不是注销?

Future<FirebaseUser> logout() async 
    try 
      print('logout ran');
      print(currentUser);       //THIS RETURNS AN INSTANCE OF USER
      await _auth.signOut();

      await _googleSignIn.signOut();
      //The problem is with this, you need to await this
      //print(currentUser);     //THIS *ALSO* RETURNS AN INSTANCE OF USER
      var user = await currentUser;
      print(user); //prints null
      notifyListeners();
      return currentUser;
     catch (error) 
      print('an error');
    
  

【讨论】:

久经考验。同样的问题。 你有一个currentUser的return语句,它是否也返回currentUser而不是null? 是的,如果您在谈论 currentUser getter,那么我尝试在 getter 中返回之前打印响应,它会返回一个用户实例【参考方案2】:

我遇到了类似的问题,并通过将 null 分配给 currentUser 来解决它。现在可以正常使用了!

        case R.id.main_logout_option:
            updateUserStatus(getString(R.string.strOffline));
            mAuth.signOut();
            currentUser = null;
            goToLoginActivity();
            break;

【讨论】:

以上是关于Firebase.instance.signOut() 未将 currentUser() 设置为 null的主要内容,如果未能解决你的问题,请参考以下文章