Maybe I am misunderstanding the use of buildWhen in the BlocConsumer but seems like it is ignoring the conditions and building on every state.
I have the following logic in my BlocConsumer:
BlocConsumer<PagerBloc, PagerState>(
bloc: pagerBloc,
listener: (context, state) {},
buildWhen: (previousState, newState) {
return newState is InitialPagerState || newState is ServicesHaveChangedState;
},
builder: (context, state) {
print("State in builder is $state");
if (state is InitialPagerState) {
return ServicesStatusWidget(state.data);
}
if (state is ServicesHaveChangedState) {
return ServicesStatusWidget(state.data);
}
return Container();
},
);
With this code I can see in the logs the following prints:
State in builder is Instance of 'InitialPagerState' //which is correct
State in builder is Instance of 'AlarmClickedState' // which for my understanding should not even be printed due to the
buildWhencondition blocking thebuilderbeing called for this state. And in this wrong case thereturn Container()is being executd which is non desired.
What am I doing wrong here?
Hi @andreddie81 馃憢
Thanks for opening an issue!
Are you able to share a sample app which reproduces the issue? Just to be clear, buildWhen does not guarantee that the widget will never be rebuilt. It just guarantees the widget won't be rebuilt when the bloc state changes. There are many other reasons for widgets to need to rebuild, however, like if the keyboard is shown/hidden, if you navigate to another screen, etc...
Let me know if that helps and if not please provide a link to a sample app so that I can debug the issue locally, thanks 馃憤
Thanks for your fast answer. Now that you mention....the widget is placed inside of a PageView widget, which might be the root of the problem.
Thanks for the support!
No problem! I'll close this for now but feel free to add any additional questions/comments and I'm happy to continue the conversation 馃槃
Most helpful comment
Thanks for your fast answer. Now that you mention....the widget is placed inside of a PageView widget, which might be the root of the problem.
Thanks for the support!