API Reference
AuthState
Complete API reference for AuthState, the discriminated union emitted by authStateChanges.
Overview
AuthState is a small value object with three mutually exclusive states: authenticated, unauthenticated, or error. It's the type emitted by authStateChanges.
class AuthState extends Equatable {
final AuthStateType type;
final AuthUser? user; // non-null only when type == authenticated
final String? error; // non-null only when type == error
}
enum AuthStateType { authenticated, unauthenticated, error }
Because AuthState extends Equatable, identical consecutive states are deduplicated on the stream, no spurious StreamBuilder rebuilds when nothing actually changed.
Factories
AuthState has no public constructor; build one of these instead:
factory AuthState.authenticated(AuthUser user)
factory AuthState.unauthenticated()
factory AuthState.error(String error)
final state = AuthState.authenticated(currentUser);
assert(state.isAuthenticated);
assert(state.user != null);
Accessors
bool get isAuthenticated; // type == AuthStateType.authenticated
bool get isUnauthenticated; // type == AuthStateType.unauthenticated
bool get hasError; // type == AuthStateType.error
Pattern matching
switch (state.type) {
case AuthStateType.authenticated:
return DashboardPage(user: state.user!);
case AuthStateType.unauthenticated:
return const LoginPage();
case AuthStateType.error:
return ErrorPage(message: state.error!);
}
When each state is emitted
| Trigger | Resulting state |
|---|---|
initialize() restores a persisted session | authenticated(user) |
initialize() finds no session | unauthenticated() |
signIn() succeeds | authenticated(user) |
signOut() completes | unauthenticated(), or authenticated(nextUser) if another account remains |
| Token refresh succeeds | authenticated(user) (same user, fresh token) |
| Token refresh is exhausted or denied | unauthenticated() |
signIn() fails | error(message) |