Bloc: BlocConsumer: builder being called ignoring buildWhen conditions

Created on 2 Jun 2020  路  3Comments  路  Source: felangel/bloc

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 buildWhen condition blocking the builder being called for this state. And in this wrong case the return Container() is being executd which is non desired.

What am I doing wrong here?

question

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!

All 3 comments

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 馃槃

Was this page helpful?
0 / 5 - 0 ratings

Related issues

craiglabenz picture craiglabenz  路  3Comments

rsnider19 picture rsnider19  路  3Comments

Reidond picture Reidond  路  3Comments

hivesey picture hivesey  路  3Comments

komapeb picture komapeb  路  3Comments