Search code examples
flutterperformancesetstatestatefulwidget

Flutter setState Function


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)


Solution

  • 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.