Reputation: 786
In Flutter, which way has the more performance over high load handling?
Let's say that we are listening to a stream which emits around 1000 msgs per second. Which way will have more performance?
1) Using setState() like
stream.listen((msg) => setState(() => _msg = msg)
then in the build method
return Text(_msg);
2) Using stream builder
StreamBuilder(
stream: stream,
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text(snapshot.data)
);
}
},
)),
Upvotes: 4
Views: 4167
Reputation: 8560
According with flutter docs:
When setState() is called on a State, all descendent widgets will rebuild. Therefore, localize the setState() call to the part of the subtree whose UI actually needs to change. Avoid calling setState() high up in the tree if the change is contained to a small part of the tree.
Therefore in your particular case may you will haven't performance differences, however in large widget trees StreamBuilder will isolate your widget avoiding rebuild in entire tree.
I also recommend you to check you specific scenario with "DevTools Widget Inspector Page". It will give you a real time analysis, including GPU time, memory consumption, performance and much more.
Upvotes: 6
Reputation: 277037
Both are identical (modulo the fact that StreamBuilder
does it in a different Widget
).
To begin with, StreamBuilder
internally does that same setState
call.
Upvotes: 4