Bloc: Builder gets executed even if buildWhen is false

Created on 11 Nov 2020  ·  5Comments  ·  Source: felangel/bloc

Hi, I'm trying to use BLoC as a state management system for my project but not really understand the build when parameter.
For example, I have the following code:

BlocConsumer<ExampleBloc, ExampleState>(
  listenWhen: (previous, current) => false,
  listener: (context, state) {
    log('listen fired');
  },
  buildWhen: (previous, current) => false,
  builder: (context, state) {
    log('build fired');
    if (state is ExampleInitial) return Text('Initial');
    if (state is ExampleLoading) return Text('Loading');
    if (state is ExampleSuccess) return Text('Success');
    return Text('No data');
  },
)

Once the state has changed the listener is not going to be executed because of listenWhen flag, however, the builder is executed regardless of what's inside buildWhen flag. What changed is the only setState is not executed i.e. Text widget is not re-rendered but the builder function is still executed each time. On the other side listener callback's not being executed if false. Is it expected?

bug flutter_bloc

Most helpful comment

Fixed as part of #1916 and released in flutter_bloc v6.1.1 🎉

All 5 comments

I am facing the same issue with BlocConsumer buildWhen/builder since my project has been upgraded to flutter_bloc 6.1.0.

But BlocBuilder buildWhen/builder seem to work as expected.

It's quite boring because latest version of VSCode bloc extension requires flutter_bloc 6.1.0 so I need to disable this extension after downgrading flutter_bloc.

Flutter 1.23.0-18.1.pre • channel beta • https://github.com/flutter/flutter.git Framework • revision 198df796aa (4 weeks ago) • 2020-10-15 12:04:33 -0700 Engine • revision 1d12d82d9c Tools • Dart 2.11.0 (build 2.11.0-213.1.beta)

Thanks for posting that @alexisbg I tried to downgrade to the 6.0.0 version and the builder function doesn't get executed.
It sounds like a bug at this point. I pushed the minimum reproducible version here
6.1.0
6.0.0

There is a simple workaround If your project does not require too many BlocConsumer refactoring: you can replace a BlocConsumer by a BlocBuilder nested into a BlocListener. And it's working with flutter_bloc 6.1.0 too.

Thanks for pointing this out! It’s definitely a bug and I’ll have a fix in the next few hours 👍

Fixed as part of #1916 and released in flutter_bloc v6.1.1 🎉

Was this page helpful?
0 / 5 - 0 ratings