State management In Flutter Application
With the heading, we all have questions about what is State management in Flutter.
In Flutter, state management refers to how an app manages and updates its state, or data, as it flows through the app. There are various approaches to state management in Flutter, and choosing the best approach depends on the specific needs of the app.
%[https://www.spec-india.com/wp-content/uploads/2021/02/Flutter-state.gif]
With the understanding of State management, let's understand two important widgets:
Stateless Widget
Stateless Widgets are immutable and remain unchanged throughout the life cycle. An example would be Text("Data")
.
Stateful Widget
Stateless Widgets are mutable and can be changed throughout the life cycle. An example would be
Inkwell, Check-Box, Radio Button etc
.When it comes to Stateful Widget it will maintain internally its own status and can re-render if the input data changes or if Widget’s state changes.
If there are very fewer widgets in UI it's fine to use Stateful Widget but when we have a lot of widgets available and if we use the Stateful Widget there will be a lot of burden on RAM as refresh a whole UI will re-build which intern reduces the speed and load time which leads application might hang or unresponsive.
SetState() and initState(), dispose() - Basically we use this method when we want to manage the states using Stateful Widget
Now do we have a better solution for this to manage the state?
Upcourese, in Flutter we can manage the stats in different ways, below are the packages that are used by different developers as per their use cases:
Provider https://pub.dev/packages/provider
setState
InheritedWidget and InheritedModel
Redux and Fish-Redux
BLoc/Rx https://pub.dev/packages/bloc
GetIt
MobX
Binder
Let's understand one example, whenever I want to build an application that is one screen that basically displays the product information it's fine to use the Stateless Widget. When it comes to Stateful Widget we can use it when we can update the cart items dynamically whenever the user added the items.
Please follow me for more information about the Flutter Topic and usage of Widgets with detailed examples.
If you have any feedback, you can send it to teamrahul.naik@gmail.com.