Bloc: [QUESTION] best practices for not redrawing widget that doesn't have state change

Created on 7 Feb 2020  路  2Comments  路  Source: felangel/bloc

Hi @felangel is it the best practices to have multiple blocs for one page for each widget that we
only want to change ?

for example, if I have pages that have 3 widget:
widget A
widget B
widget C

only widget B and C (different state) that will be rebuild if there's some state changed.
should I create 2 different bloc for each widget B and C, and wrap them in it ? in this case it will be widgetABloc and widgetBBloc.
I personally doesn't want to rebuild widget A for there's no changes for it.

thank you.

question

Most helpful comment

If you feel you wanna optimize anything, do it.

Of course, if there is no caching of some kind, all the widgets inside BlocBuilder will be redrawn on state updates. They might be (and probably will be) also redrawn anytime Flutter feels like it. So I would not go to over-optimizing this, because it doesn't really come with any major benefit anyway. At least if you don't have thousands of widgets there, and at that point you probably using some ListView.builder or so anyway.

All 2 comments

If you feel you wanna optimize anything, do it.

Of course, if there is no caching of some kind, all the widgets inside BlocBuilder will be redrawn on state updates. They might be (and probably will be) also redrawn anytime Flutter feels like it. So I would not go to over-optimizing this, because it doesn't really come with any major benefit anyway. At least if you don't have thousands of widgets there, and at that point you probably using some ListView.builder or so anyway.

Hi @gabrielintang 馃憢
Thanks for opening an issue!

There are several things you can do depending on the situation:

  • If WidgetA does not require data don't include it within BlocBuilder (only wrap the widgets that need to rebuild)
  • If WidgetA needs a different part of the state than WidgetB you can add wrap each in their own BlocBuilder and add a condition to each
  • if WidgetA and WidgetB are different features (independent of each other) you should create separate blocs to manage the states respectively

With all that being said, please don't spend too much time trying to optimize for rebuilds unless you are having performance problems (as @tenhobi mentioned).

Hope that helps!

Closing for now but feel free to comment with additional questions and I'm happy to continue the conversation 馃憤

Was this page helpful?
0 / 5 - 0 ratings