简体   繁体   中英

Flutter: How to use FirebaseAuth idTokenChange?

If I logged in, go to the main page without log in again.

like this

FirebaseAuth _auth = FirebaseAuth.instance;
  User firebaseuser = _auth.currentUser;
  if (firebaseuser == null) {
    return runApp(MaterialApp(home: login()));
  } else {
    return runApp(MaterialApp(home: MainPage()));
  }

however currentUser description says

You should not use this getter to determine the users current state, instead use [authStateChanges], [idTokenChanges] or [userChanges] to subscribe to updates.

So I decided to use idTokenChanges .

because it says

Notifies about changes to the user's sign-in state (such as sign-in or sign-out) and also token refresh events.

And I made it like this

Stream<User> loginstate = await FirebaseAuth.instance.idTokenChanges();

But i don't know how to use it.

You should use authStateChanges for only user sign-in state updates, not idTokenChanges . There is an example working with its stream in the documentation :

FirebaseAuth.instance
  .authStateChanges()
  .listen((User user) {
    if (user == null) {
      print('User is currently signed out!');
    } else {
      print('User is signed in!');
    }
  });

idTokenChanges is for getting updates to the user's ID token, which will refresh every hour.

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