You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Session manager listeners are called multiple times, even when nothing changes #1651
When a user is already logged in, the session manager listeners are called twice on app startup, both times with the login state being the same (once from _loadSharedPrefs, then once from refreshSession). This breaks the ChangeNotifier convention of only notifying listeners when something actually changes.
This is kind of a pain, because my login system relies on being notified just once when a user is logged in -- so I have to de-dup the change listener calls, using ValueNotifier (which properly follows the ChangeNotifier contract of only notifying when changes are actually made):
finalSessionManager sessionManager =SessionManager(caller: client.modules.auth)
..addListener(() =>
_sessionManagerSignedInUser.value = sessionManager.signedInUser?.id);
// Need a separate ValueNotifier to de-dup session manager updatesfinal _sessionManagerSignedInUser =ValueNotifier<int?>(null)
..addListener(_sessionManagerSignedInStateListener);
void_sessionManagerSignedInUserListener() async {
if (_sessionManagerSignedInUser.value !=null) {
// ...
}
}
@lukehutch We could implement deep-equality on UserInfo in order to avoid the listener being called without any changes.
I think your mapping into a ValueNotifier (or similarly a ValueListenable<int?> userId) still does make sense though, as the UserInfo object is quite large, and there could be changes to it that are just "minor" updates and not a login/logout: for example when the user object retrieved from the server differs from the one read from local storage.
In that case (e.g. a name or profile image change) we would still need to notify the outside of that change.
So I think you approach of focusing only on the userId or a isLoggedIn state is a good one for many apps, to only update specifically when the primary condition changes.
The upcoming serverpod_auth_session_flutter package will offer a new SessionManager implementation that uses a ValueNotifier for the "auth info" (user ID + scopes), which will only update on login/logout, when those values actually change.
Activity
tp commentedon Jan 27, 2025
@lukehutch We could implement deep-equality on
UserInfoin order to avoid thelistenerbeing called without any changes.I think your mapping into a
ValueNotifier(or similarly aValueListenable<int?> userId) still does make sense though, as theUserInfoobject is quite large, and there could be changes to it that are just "minor" updates and not a login/logout: for example when the user object retrieved from the server differs from the one read from local storage.In that case (e.g. a name or profile image change) we would still need to notify the outside of that change.
So I think you approach of focusing only on the
userIdor aisLoggedInstate is a good one for many apps, to only update specifically when the primary condition changes.tp commentedon Jun 27, 2025
The upcoming
serverpod_auth_session_flutterpackage will offer a newSessionManagerimplementation that uses aValueNotifierfor the "auth info" (user ID + scopes), which will only update on login/logout, when those values actually change.