Start a Project

Riverpod State Management Flutter

Riverpod State Management Flutter, State Management in Flutter is an important topic not to be overlooked. Obviously, you can’t just use set state(); it throughout a project consisting of 3000 lines of code over 20 different files! There are many approaches in Flutter that you can use for managing your state and for dependency injection.

Why use providers?

Looking for a trusted Flutter app development company? We’ve got you covered.

Features of Riverpod.

Now let’s understand the Riverpod.

Riverpod State Management Flutter.

As I mentioned, Riverpod state management is an improved version of Providers.
Providers play an important role in Riverport.

Providers are the most important part of a Riverpod application. A provider is an object that encapsulates a piece of state and allows listening to that state.

Creating a provider.

The most common usage is to declare them as global constants like so.

The final name is the declaration of the variable. The Provider(); comes from Riverpod Class. "Hello Flutter" refers to the string that our Provider is returning. You can pass a stringint, or any other data type.

Installing Riverpod.

Now run this command in terminal $ flutter pub get

There are 3 types of riverpod:

  1. riverpod to be used only with Dart
  2. flutter_riverpod to be used only Flutter and riverpod
  3. riverpod_hooks when we use flutter_hooks in our flutter Project.

We are gonna use flutter_riverpod.

Let’s make a Counter App

MyAppWidget is containing only Title and Body With FloatingActionButton.

For now, we’re using StateProvider for updating the state.

Wrap MyApp() in ProviderScope() for using providers.

let’s create a provider for our counter app.

the counter provider will return only integer type data.

Reading a Provider

First and foremost, before reading a provider, we need to obtain a “ref” object.

This object is what allows us to interact with providers, be it from a widget or another provider.

convert StatelessWidget to Consumerwidget.

Add WidgetRef to listen to references of the provider.

ref.read(counter.state).state++; this will increment the value of counter.

And this will increment the value of the count and this will rebuild the whole widget tree.

And this is not a good use of the provider, we just want to update the only TextWidget value. For that wrap Text Widget with Consumer.

Now this will only rebuild the TextWidget.

And Congrats🎉🎉, you made your first Riverpod App(if it runs successfullly🤭😉).

Conclusion:

In this article, I have explained the basic use of Riverpod state management in a flutter; you can modify this code according to your choice. This was a small introduction to Riverpod With Flutter On User Interaction from my side, and it’s working using Flutter.

That’s all for today.

If you want to learn more about Riverpod in flutter please go and check their official page:
https://riverpod.dev/

You can read more blogs from Mobikul on this website: https://mobikul.com/blog/

Exit mobile version