Home > database >  Flutter setState Function
Flutter setState Function

Time:09-14

I noticed the more you got variables in your statefulWidget, the more the setState function takes time to complete. Making the app kinda slow, and this is quite annoying.

Is there a way to change state for Only one variable, please? I mean something like this:

setState(...varToUpdate)

CodePudding user response:

No. setState by design is rebuilding all widgets that depend on the state on which the method os being called.

If you refactor your monolithic widget into sub-widgets, you can have finer-grain control over what gets rebuilt. Also, you should look into a state management solution like RiverPod to be able to narrow down "consumers" to be associated with their triggers, which helps tremendously.

Also, if your build is expensive, you are doing something wrong. A build should be cheap, capable of being performed 60 times per second with no I/O or expensive calculations.

CodePudding user response:

No there isn't, since setState rebuilds the whole widget. It updates all variables.

Performance is just slow in debug mode. Once you build your app in release mode it should be faster.

  • Related