Home > Mobile >  Confusion on FlutterFire's FirebaseAuth.currentUser documentation
Confusion on FlutterFire's FirebaseAuth.currentUser documentation

Time:01-11

The currentUser getter in notification_manager.dart is:

  /// Returns the current [User] if they are currently signed-in, or `null` if
  /// not.
  // 
  /// You should not use this getter to determine the users current state,
  /// instead use [authStateChanges], [idTokenChanges] or [userChanges] to
  /// subscribe to updates.
  User? get currentUser {
    if (_delegate.currentUser != null) {
      return User._(this, _delegate.currentUser!);
    }

    return null;
  }

I'm wondering if it really means to say "You should not use this getter to poll the users current state". If it really is bad practice to use this function to get the current state could someone please explain the reason for this? Is the currentUser not automatically updated?

CodePudding user response:

I use authStateChanges in code where I need to respond to changes in authentication state, but I use currentUser in code that needs to know now who the current user is, or if there's a current user.

I'd recommend everyone to do the same, but we see users so often using currentUser everywhere in their code, and it then dealing with failures, where authStateChanges and a StreamBuilder would work beautifully, that I guess the authors of this documentation wanted to be overly explicit.

If you have better wording in mind, I'm sure the authors would love a PR. :)

CodePudding user response:

This is not current user state, this is current user.

The currentUser get method don't determine the current user state.

The state must be variable in nature.

If you want to be informed when user changed, you must listen authStateChanges to determine current user and user state.

  •  Tags:  
  • Related