Pick when to watch, read, or listen
Provider gives you a synchronous value. AsyncProvider wraps work from a Future or Stream in AsyncValue and manages its lifecycle.
Which provider to use
Choose a provider based on where the value comes from and when it changes.
Provider<T>
A synchronous value or state you calculate locally
AsyncProvider<T>
An AsyncValue<T> backed by a Future or Stream
provider.future
A selector that gives you a Provider<Future<T>>
Choose a provider for the work
Use AsyncProvider for requests and streams, and Provider for values that are already available.
Use the right way to read a provider
Use watch in build to redraw on changes, read to get the current value once, and listen to handle effects when values change.
watch / read / listen
ref.watch(provider) Rebuild when the value changes.
ref.read(provider) Get the current value once.
container.listen(...) Run a callback on updates. Use fireImmediately to get the current value too.
Connect a Future or Stream to AsyncProvider
Return a Future from AsyncProvider or attach a Stream inside it with ref.emit(stream).
final currentUser = AsyncProvider<User>((ref) async {
final api = ref.watch(apiProvider);
return api.me();
});
final liveUser = AsyncProvider<User>((ref) {
final stream = ref.watch(apiProvider).live();
ref.emit(stream);
return const User(name: 'Loading...');
}, autoDispose: true, autoDisposeDelay: const Duration(milliseconds: 250));
// Await as Future
final user = await ref.watch(currentUser.future);
Where to go next
Run mutations
Define writes as provider methods and run them using ref.invoke.
Learn to run mutationsUse providers in Flutter
Find out when to use Consumer, ConsumerWidget, or ConsumerStatefulWidget.
See the Flutter API