Is setState() heavy?

197 views
Skip to first unread message

Jaxon Du

unread,
Aug 2, 2018, 6:46:16 AM8/2/18
to Flutter Dev
Let say top of my app hierachy is a stateful widget which has many child widgets. When a setState() is called, the build() method of the widget will be called, which means all the many child widgets will be re-created. Isn't this expensive operation?

How about creating new MaterialApp() or new Scaffold() widgets inside a StreamBuilder? Is it something we should avoid?

Dale King

unread,
Aug 2, 2018, 4:31:18 PM8/2/18
to Flutter Dev
Certain song by the Hollies comes to mind with the title ;-). (It ain't heavy, it's my brother)

That way can be heavy and is not recommended for serious apps. Much has been written about alternative patterns for this like Redux and BLoC and a search on those terms with flutter should provide you with a cornucopia of information. I will point you to a few videos that talk about these:

Ian Hickson

unread,
Aug 2, 2018, 6:43:03 PM8/2/18
to Dale King, Flutter Dev
setState itself is more or less free, but it schedules the current widget to be rebuilt, which will rebuild any widgets that are created by that widget.

Rebuilding the MaterialApp is probably pretty cheap, compared to what you might imagine.

Dale had some good pointers though. Generally I would recommend putting your app state outside of the widget tree, and then creating custom stateful widgets that are very localized and just update the parts that need updating, when the app state changes.

--
You received this message because you are subscribed to the Google Groups "Flutter Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--

--
Ian Hickson

😸

Gregg Reynolds

unread,
Aug 2, 2018, 6:47:22 PM8/2/18
to Ian Hickson, Dale King, Flutter Dev
Can somebody from the flutter team weigh in? I was under the impression the the Build machinery was highly optimized, such that nothing gets rebuilt unless it needs it (i.e. dirty) .

Gregg

Ian Hickson

unread,
Aug 2, 2018, 7:45:44 PM8/2/18
to Gregg Reynolds, Dale King, Flutter Dev
setState is the mechanism by which widgets are marked dirty, so once you call setState, it is assumed that it needs it. If you return a different widget from your `build` method, then that widget is also assumed to be dirty, since it is not the same widget as before.

One way you can make builds more efficient is by making sure to return the same widget from your build method. We use this a lot, for example, that's why `const` widgets are more efficient (they're always the same instance). It's also why AnimatedBuilder takes a `child` argument and a `builder` argument, and then passes the `child` to the builder: the builder can then make sure to always use the same child instance in its output each frame, thus limiting the parts that are dirtied (and rebuilt) to the parts that changed (the parts created by the builder itself).

HTH.

(I'm from the Flutter team.)
Reply all
Reply to author
Forward
0 new messages