felangel / bloc

A predictable state management library that helps implement the BLoC design pattern

Home Page:https://bloclibrary.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

docs: Please help out with BlocBuilder and buildWhen understanding.

oakstair opened this issue · comments

BlocBuilder and buildWhen issue

I am aware that this is probably not a bug but I have a hard time to understand how to solve my issue.

E.g. see #3229

I started out some days ago to clean up a quite complex screen by using flutter_bloc. See example below.

The screen is called TournamentScreen. See example below.

So I created TournamentScreenBloc, TournamentScreenState and TournamentScreenEvent and moved all busines logic into my BLOC. Super nice indeed.

The screen is dividend into three sections left, middle and right.
The right and left are PageViews containing various what we call monitors.

Now to my issue ...

I would like to benefit from BlocBuilder's buildWhen parameter to avoid redrawing areas of the screen.

E.g. the tournament clock is ticking each second but in that case there is no need to rebuild the players monitor.
(In most cases ... e.g. if blind level is increased we will have redraw it)

But I fail with this. The screen is built each time as can bee seen from log below.

flutter: 11:44:47.806: [TournamentScreenBloc] ---->>>> Instance of 'TournamentScreenEventClockTick'
flutter: 11:44:47.809: [PlayersMonitor] buildWhen: false
flutter: 11:44:47.830: [PlayersMonitor] build()
flutter: 11:44:48.808: [TournamentScreenBloc] ---->>>> Instance of 'TournamentScreenEventClockTick'
flutter: 11:44:48.813: [PlayersMonitor] buildWhen: false
flutter: 11:44:48.832: [PlayersMonitor] build()
flutter: 11:44:49.806: [TournamentScreenBloc] ---->>>> Instance of 'TournamentScreenEventClockTick'
flutter: 11:44:49.808: [PlayersMonitor] buildWhen: false
flutter: 11:44:49.830: [PlayersMonitor] build()
flutter: 11:44:50.807: [TournamentScreenBloc] ---->>>> Instance of 'TournamentScreenEventClockTick'
flutter: 11:44:50.809: [PlayersMonitor] buildWhen: false
flutter: 11:44:50.828: [PlayersMonitor] build()
...

Note: The code is too much to include here but in overall I have a BlocProvider on top of the TournamentScreen which uses BlocProvider.

The monitors are also build with BlocProvider.

Thanks in advance for any help with this issue!

Skärmavbild 2024-02-17 kl  11 33 50

This morning it started to work after that I added "const" before the call to my monitor constructors!