Installation
Key Differences from flutter_riverpod
- No
ConsumerorConsumerWidget- usecontext.watch()directly - Additional
syncoption to synchronize providers between server and client - Works with any component type (no special base classes required)
Basic Usage
Defining Providers
Define providers the same way as in Riverpod:Accessing Providers
Access providers using context extensions on any component:Context Extension Methods
All standard Riverpod methods are available onBuildContext:
Provider Scope
Wrap your app withProviderScope:
Builder Pattern for Selective Rebuilds
UseBuilder to limit rebuilds to specific parts of your component tree:
Syncing Provider State (Server to Client)
One of jaspr_riverpod’s unique features is the ability to sync provider state from server to client:How Syncing Works
- During server-side rendering, the provider value is read and serialized
- The value is embedded in the HTML sent to the client
- On the client, the value is deserialized
- The provider is overridden with the server value
- Accessing the provider on the client returns the server value immediately
Sync Configuration
ThesyncWith() method accepts:
Custom Codec Example
Supported Provider Types
The following providers support syncing:NotifierProviderAsyncNotifierProviderProviderFutureProviderStreamProviderStateProvider
Awaiting Async Providers
When syncing async providers (FutureProvider, StreamProvider, AsyncNotifier), the ProviderScope waits for them to complete before rendering on the server:
Provider Scoping
Sync overrides only propagate when defined on the rootProviderScope. For scoped providers, set dependencies:
Family Providers
Use family providers for parameterized state:Auto Dispose
Automatically dispose providers when no longer used:Why Context Extensions?
Context extensions (context.watch) offer several advantages:
- Less boilerplate (no special widget types)
- More flexible (works with any component)
- Easier to use (natural API)
context.watch feasible.