Overview
App Courier uses the Provider pattern for state management. All business logic is encapsulated in Provider classes that extendChangeNotifier, allowing UI components to reactively update when state changes.
Provider Architecture
Provider Initialization
Providers are initialized at app startup inmain.dart using MultiProvider:
Provider Structure
All providers follow a consistent pattern. Here’s the structure ofEncomiendasProvider:
State Flow Pattern
Every async operation follows this flow:Using Providers in UI
Reading State
UseProvider.of or context.watch to read provider state:
Calling Provider Methods
Usecontext.read for calling methods to avoid unnecessary rebuilds:
Conditional UI Based on State
Provider Examples
CustomersProvider
Manages customer data and operations:EncomiendasProvider
Manages encomienda state with complex operations:Common State Patterns
Pattern 1: List Management
Pattern 2: Single Item Management
Pattern 3: Form State with Validation
Pattern 4: Filter State Persistence
Error Handling in Providers
Strategy 1: Store Error, Don’t Re-throw
Strategy 2: Store Error and Re-throw
Strategy 3: Clear Previous Errors
Best Practices
Always call notifyListeners() after state changes
Always call notifyListeners() after state changes
Every time you mutate state that affects the UI, call
notifyListeners() to trigger widget rebuilds.Use private variables with public getters
Use private variables with public getters
Encapsulate state to prevent external mutation:
Set loading state before async operations
Set loading state before async operations
Provide immediate user feedback:
Use context.read for actions, context.watch for state
Use context.read for actions, context.watch for state