简体   繁体   中英

FirebaseAuth current user is not returning null after calling sign out?

I am trying to logout user and switch the widgets but after calling the following function -

void logoutUser() async {
    await FirebaseAuth.instance.signOut();
}

If I check for the current user, it's returning user object but with null id -

FirebaseAuth.instance.currentUser()

I try to kick the user out to main.dart after logout which checks if user is signed in or not and loads a correct widget. Does anyone have any idea why currentUser() isn't returning null after calling signOut() ?

_auth.currentUser() is probably returning an anonymous FirebaseUser object . Check the isAnonymous property.

Example :

auth.currentUser().then((user) {
  if (user == null || user.isAnonymous) {
    // what will you do?
    return;
  }
  setState(() {
    _uid = user.uid;
  });
});

Yet, I would highly recommend to monitor the onAuthStateChanged stream instead . This way you will be informed when the user logs in or logs out immediately.

Check this article , it covers it in depth.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM