Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: What is the need of state management libraries when I can use global variables and use setState instead?

I understand that bloc pattern or getX would provide extra features other than just state management in flutter, but my main concern is why should I use bloc or getX or any other state management library instead of just using setState with global variables?

I do also understand that if I update the value of a global variable from a child class and navigate back to the parent then it's state will not be updated but to overcome this issue, I just called the setState() of parent from the child class. Is there anything wrong with my approach and if yes then what?

like image 831
Sahil Avatar asked Oct 25 '25 18:10

Sahil


1 Answers

Whenever you calling setState() function, your entire Stateful or Stateless Widget will rebuild again. It makes your application's performace to low level.

For Eg: You have a Stateless Widget like Following

Scaffold(
 body: Column(
  children: [
   Container(
    child: Text(SomeText);
   ),
   FlatButton(
    onPressed: (){
     seState({
      //change Text Function
     });
    }
   )
  ]
 )
)

Here, when you click button and its function is to change value of SomeText,Now these all code will rebuild and yes, its replace SomeText with new value. But it is not just changed it but it rebuild entire widget to change it.

Here Instead of setState() method, if you are using any state management libraries, then it will only change value of SomeText without affecting other widgets.

as a beginer You can try Provider

like image 124
ijas Avatar answered Oct 28 '25 08:10

ijas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!