State Management with Riverpod
- flutter pub add flutter_riverpod
- flutter pub add riverpod_annotation
- flutter pub add dev:riverpod_generator
- flutter pub add dev:build_runner
- flutter pub add dev:custom_lint
- flutter pub add dev:riverpod_lint
###Enabling riverpod_lint/custom_lint : add to analysis_options.yaml
analyzer:
plugins:
- custom_lint
Before we start making network requests, make sure that ProviderScope is added at the root of the application.
void main() {
runApp(
// To install Riverpod, we need to add this widget above everything else.
// This should not be inside "MyApp" but as direct parameter to "runApp".
ProviderScope(
child: MyApp(),
),
);
}In Riverpod, business logic is placed inside "providers". A provider is a super-powered function. They behave like normal functions, with the added benefits of:
- being cached
- offering default error/loading handling
- being listenable
- automatically re-executing when some data changes
-
Provider is the most basic of all providers. It creates a value... And that's about it.
Provider is typically used for:
caching computations exposing a value to other providers (such as a Repository/HttpClient). offering a way for tests or widgets to override a value. reducing rebuilds of providers/widgets without having to use select.
-
NotifierProvider is a provider that is used to listen to and expose a Notifier. AsyncNotifierProvider is a provider that is used to listen to and expose an AsyncNotifier. AsyncNotifier is a Notifier that can be asynchronously initialized. (Async)NotifierProvider along with (Async)Notifier is Riverpod's recommended solution for managing state which may change in reaction to a user interaction.
It is typically used for:
exposing a state which can change over time after reacting to custom events. centralizing the logic for modifying some state (aka "business logic") in a single place, improving maintainability over time
-
FutureProvider is the equivalent of Provider but for asynchronous code.
FutureProvider is typically used for:
performing and caching asynchronous operations (such as network requests) nicely handling error/loading states of asynchronous operations combining multiple asynchronous values into another value
-
StreamProvider is similar to FutureProvider but for Streams instead of Futures.
### cli to generate riverpod files
dart run build_runner watch --delete-conflicting-outputs
flutter pub add flutter_hooks
