RiverPod State Management In Flutter

RiverPod State Management In Flutter :

  • Created by RemiRousselet.
  • Its compile-time safe and testable.It doesn’t have a dependency on the Flutter SDK.
  • Riverpod can be used to enforce a unidirectional data flow with immutable model classes.
  • We create global providers that be accessed by reference. In this sense, Riverpod does not depend on the widget tree, and works more like a service locator.
  • Widgets can access these providers with a ScopedReader, either via Consumer or ConsumerWidget.
  • Riverpoddeclare your providers globally.
RiverPod State Management In Flutter
  • How RiverPod works?

You create the provider and how to access in flutter:

finaluserProviderState = userProvider(userProviderState.state);

  • When id null then uuid get.
var _uuid = UuId();
classTodo{
final String id;
final String description; 

TODO({this.description, String id}) : id = id ?? _uuid.v4();
}
  • Advantages of Riverpod :
  • Declare provider and provider class in one file without convoluting app root
  • provider can be access from anywhere using ConsumerWidget or context.read
  • provider can easily consume another provider using ProviderReference
  • There can be multiple providers of the same type.

pubspec.yaml

dependencies:
flutter:
sdk: flutter
flutter_riverpod:
OR
hooks_riverpod:
flutter_hooks:
  1. ProviderScope:The Riverpod package comes with just a single InheritedWidget that needs to be placed above the whole widget tree called ProviderScope. It’s responsible for holding something called a ProviderContainer which is in turn responsible for storing the state of individual Provider objects.
    1. Our whole application,we need to wrap the parent widget with ProviderScope wrapper.
    2. Use: We will be able to access and manage states from anywhere in our app.
  2. Notifierclass:Notifierclass now which will help us manage the state and data throughout our app.
  3. StateProvider:Listened to and update the UI when their data changes. This type of provider is ideal for simple data types such as flags or filters that a component needs.
    1. Creates a StateNotifier and exposes its current state. This is my go to alternative to ChangeNotifierProvider. It allows us to manipulate advanced states without exposing a state to the outside like we did with count earlier.
finalselectedButtonProvider = StateProvider<String>((ref) =>'');
finalisRedProvider = Provider<bool>((ref) { 
final color = ref.watch(selectedButtonProvider);
returncolor.state == 'red';
});

4. ConsumerWidget: Riverpod has ConsumerWidget which is basically a stateless widget that allows us to listen to a provider, so the UI can automatically update the component when needed.  Because we extend ConsumerWidget our build method would require a ScopedReader object. This is what allows us to listen to a provider using watch

5.ScopedReader:ScopedReader function into the widget’s build method.We use the ScopedReader to watch the provider and rebuild the widget if any change occurs, the convention is to give this parameter a name “watch”.

6.watch: called the function build method. Riverpod is that we can watch a provider’s value and rebuild all dependent providers and widgets when the value changes.

7.read: read method is used to read a provider without listening to it.

8.ChangeNotifierProvider: The downsides are that ChangeNotifierProvider’s data is mutable and can be accessed and overwritten from different places.

classCounterextendsChangeNotifier {
int count;
Counter([this.count = 0]);

void increment() {
count++;
notifyListeners();
}
}

finalcounterProvider = ChangeNotifierProvider<Counter>((ref) => new Counter());
final count = watch(counterProvider).count;
Text(count);
onPressed: () { context.read(counterProvider).increment(); }
or
context.read(counterProvider).count = count + 1; // New Line

9.provider: A Provider is a place to access and listen to shared data.

final provider = Provider<String>((ref) => “Hello world”);

classHelloWorldextendsConsumerWidget {
constHelloWorld({Key key}) : super(key: key);

@override
Widgetbuild(BuildContext context, ScopedReader watch) {
String text = watch(hellowWorldProvider);
returnText(text);
   }
 }

10.Stream Provider: Stream provider is a provider that is used to listen to a stream and change whenever that stream emits a new value.

11.Future Provider:FutureProvider. autoDispose is not used here but it can be used to stop any ongoing HTTP calls if a user leaves the page that made the call.

Just like stream provider we listen to the changes on the FutureProvider by calling .when on our async value and return what’s appropriate based on the data.

12.family: autoDispose is useful even if you’re not using a family modifier. In such case, disposal will be the started when the ConsumerWidget that watches a provider is disposed.

familyThe function is to add a parameter when obtaining the provider.

13.changeNotifierProviderFamily:

14.providerFamily:

15.futureProviderFamily:

16.streamProviderFamily:

17.stateNotifierProviderFamily:

18.stateProviderFamily:

19.scopedProvider:when we implement a list of Item, we need to pass in the corresponding index roughly as follows:

finalScopedProvider<int>currentProductIndex = ScopedProvider<int>(null);

20.stateNotifier:  A listener that can be added to a StateNotifier using.This callback receives the current StateNotifier.state as parameter.

21.stateNotifierProvider:To create and mutate state we will create that extends a StateNotifier and a StateNotifierProvider to store a reference to and provide it to our application and set some initial state:

22.stateProvider:A provider that expose a value which can be modified from outside.It can be useful for very simple states, like a filter or the currently selected item – which can then be combined with other providers or accessed in multiple screens.

23.ProviderListener: If you want to monitor the state changes of the provider on the Widget Tree, you can use it ProviderListener

24.Combining providers:

DOWNLOAD SOURCE CODE :