Flutter State Management: Which is best Provider, BLoC, or Redux?
DhiWise
Posted on May 18, 2022
Flutter State management involves lots of things, from calling your app network function, asynchronously, adding up networking libraries, parsing data, and finally updating it on the main UI thread. Handling all this stuff can be painful if you don’t have enough experience in networking with Flutter.
Every developer uses different approaches to perform the same task as there can’t be one “perfect solution” to all the use cases. However, the efficiency of work done is based on the developer’s choice, knowledge about that approach, and how comfortable they are with it.
In this article, we are going to learn about Flutter state management using the Provider, BLoC, and Redux and when we should use them.
So, before starting first make sure you have a clear idea about the state management in Flutter.
The basics of Flutter state management?
When you build the app front-end, it is critical to manage the responses to each event initiated through the activities performed on the UI. In technical terms, we need to manage the state of the UI elements that change due to end-user interaction.
While managing the UI state, you might encounter the need to split the large UI into several smaller components while having effective communication among them. Moreover, all those UI components should be aware of the state of the application at all times.
The state management in Flutter can be done by using different approaches, the default setState method is the most basic approach among them. Though it is simple to use, you can’t completely depend on it.
There are other factors that you should consider while developing a Flutter app such as app complexity, scalability, and architecture. That is why you need an effective and full-proof approach for state management.
The most popular state management approaches available for Flutter include Provider, BLoC, and Redux. Let’s understand the difference between them.
Provider, BLoC, and Redux: Different Flutter state management approaches with advantages and disadvantages
Provider Pattern: A state management helper
Provider makes data values such as state model objects available to child widgets below it. The provider package is a simple and easy-to-use wrapper around the InheritedWidgets. It provides a state management technique that is used for managing the allocation and the disposal of resources.
In a widget tree, the Flutter UI can pass the data all the way down from the parent to the child where it is used. It seems efficient until your app gets more complex and bigger. Now just think about passing the data somewhere in the tree just above where we are going to use it. So you do not need to inject the data higher than needed. That is what the provider helps you achieve.
Here are a few basic classes in the provider,
1 .ChangeNotifier
It is the class extended by the other classes to send notifications if there is any change in the data of that class.
2. ChangeNotifierProvider
It is considered as a parent widget that holds the references of the ChangeNotifier that is responsible for rendering in UI (Changes happen in the ViewModel class)
3. Consumer
It holds the references of ViewModel Class that continually listens for any changes and rebuilds the child widget on which it is wrapped.
4. FutureProvider
The class listens for the Future and then passes its values to its descendants.
Advantages of using the Provider package
Simplify data allocation and disposal of resources (data)
Lazy loading of data
Reduces boilerplate code
Devtool friendly
Improves scalability
Makes InheritedWidgets easier to use.
Disadvantages of using Provider
- May accidentally call unnecessary updates. Not every change in the state of an object needs to trigger an update. However, if you are using Provider then they will trigger an update all the time when there is a change.
When to use it?
If the app is not too complex and has few models.
If you are expecting lower memory utilization and faster execution time.
If developers are not familiar with Streams.
BLoC Pattern: Business Logic Component
BLoC stands for Business Logic Component. It was created by Google to separate business logic from the presentation layer, thus facilitating developers to use code more efficiently. It is often used in conjunction with the Provider pattern.
Bloc is the state management library that helps developers to implement Bloc design in their Flutter app. It means developers must know about the app state whenever the UI interaction takes place. For example, during the data fetch operation, the app should display the data loading animation on the screen.
Therefore, app designers should consider every use case that has a state emerging from it to easily separate the presentation layer from the business logic and thus simplify state management in the app.
Bloc is an advanced class. It relies on the event to trigger the state rather than a function.
Advantages of using BLoC
Separates presentation layer from Business
Simplify app testing
Better performance for large data sizes.
Disadvantages of BLoC
Effective only if you have a big application.
You need to use streams in both directions which may create more boilerplate than Provider.
When to use it?
Best architecture if you are familiar with the streams.
Use it if you have a complex app and want to reduce memory utilization while having better performance.
Redux: A state container approach
Redux is the state container approach to state management familiar to many developers. It helps to successfully distribute the data across the widgets in a repetitive manner. It allows you to manage the app state through the unidirectional flow.
Redux enables you to structure your application in such a way that the state is extracted from the central store. It brings upstate, action, and reducer, that make up your app. Here the data in the centralized store can be accessed by any widget. So, there is no need to pass the data through the chain of widgets in a tree.
Redux is designed to prevent the bug by making State immutable and with the unidirectional data flow. The approach has proven to be a work wheel in a synchronous situation but may cause problems when you perform tasks asynchronously.
Advantages of Using Redux
Unidirectional data flow.
Immutable State.
Guarantees predictability in the synchronous situation.
Redux stores can keep the previous 5 versions of the store, so if something gets wrong, debugging can be done with ease.
Disadvantages of Using Redux
It may generate a lot of boilerplate code
Doing things asynchronously can bring unexpected side effects.
When to use it?
- If you have a global app state such as user authentication, and other preferences like language and currency.
Conclusion:
If you are building a simple application with 2 to 5 screens; inherited widgets or a provider package is enough to manage your app states. However, as your application grows you might consider using BLoC architecture or Redux.
So now you have a clear idea about what state management approach to choose for your Flutter application.
If you want to explore more about the Flutter state management approach check out this Flutter official link.
“Give your Flutter app development a kick start with DhiWise- The Multitechnology supported web and mobile app development platform for developers”
Here is why you should choose DhiWise Flutter Builder to transform your app development gig in a few clicks,
Generate clean code in minutes with Figma to Flutter functionality.
Override widgets functionality with Life cycle management.
Firebase and Supabase integrations.
Github and GitLab integration.
Full flexibility for customization.
Have full control of your code, download, modify, and run your code wherever you want.
Know more about DhiWise- The new edge ProCode app development platform and don’t forget to sign up to start Fluttering with DhiWise. 😍
Thanks for reading this article ❤
If I got something wrong 🙈, Let me know in the comments. I would love to improve.
Clap 👏 If this article helps you.
Posted on May 18, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.