Provider Package
The app uses theprovider package for state management:
pubspec.yaml:39
ChangeNotifier Pattern
Providers extendChangeNotifier to notify listeners of state changes.
Basic Structure
AuthProvider
TheAuthProvider manages authentication state.
Implementation
lib/providers/auth_provider.dart:5-20
State Properties
isLoading: Indicates async operation in progressisLoggedIn: User authentication statususer: Current user data (null if not logged in)error: Error message from last operationauthService: Access to underlying AuthService
Sending Magic Link
lib/providers/auth_provider.dart:22-38
Verifying Token
lib/providers/auth_provider.dart:40-56
Logout
lib/providers/auth_provider.dart:58-61
Clear Error
lib/providers/auth_provider.dart:63-66
Provider Setup
Providers are configured at the app root inmain.dart.
Initializing Providers
lib/main.dart:12-37
MultiProvider
UseMultiProvider to provide multiple providers:
Consuming Providers
Widgets consume provider state usingProvider.of or Consumer.
Provider.of
Access provider in build method or event handlers:Provider.of with listen: false
For one-time access without rebuilding:lib/screens/home/home_screen.dart:86
Consumer Widget
Rebuild only specific parts of the UI:Selector for Granular Updates
Listen to specific properties:State Update Pattern
Always follow this pattern when updating state:Example: Creating a RouteProvider
Using the RouteProvider
Best Practices
1. Always call notifyListeners()
After any state change:2. Use listen: false for actions
Prevent unnecessary rebuilds:3. Initialize async state
Load initial data ininitState or provider:
4. Handle loading and error states
Always provide feedback:5. Dispose resources
Clean up in provider if needed:When to Use Provider vs StatefulWidget
Use Provider when:
- State needs to be shared across multiple screens
- State should persist across navigation
- Multiple widgets need to react to the same state
- Complex state logic needs centralization
Use StatefulWidget when:
- State is local to a single widget
- State doesn’t need to be shared
- Simple UI state (e.g., dropdown selection)
- Form field values