Bloc: build when method not working

Created on 24 Jul 2020  路  3Comments  路  Source: felangel/bloc

Here is the bloc builder :

BlocBuilder(
  cubit: bloc,
  buildWhen: (LoginState oldState, LoginState newState) {
    print(oldState.toString() + "     " + newState.toString());
    if (newState is LoginRefereshState)
      return true;
    else
      return false;
  },
  builder: (BuildContext context, LoginState state) {
    print("In aaaa button");
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 26, vertical: 10),
      child: CustomButton(
        50,
        onLoginButtonTapped,
        title: StringResource.login,
        titleColor: ColorResource.WHITEFFFFFF,
        color: ColorResource.PRIMARY_COLOR,
        radius: 10,
      ),
    );
  },
);

Above is the bloc builder which is i am using in my screen, whenever i yeild the state other than LoginRefereshState, but still the above blocbuilder widget rebuilds, @felangel could you help me out with this please. Thanks in advance

question

Most helpful comment

Hi @ThalapathySiva 馃憢
Thanks for opening an issue!

buildWhen only works to prevent rebuilds triggered by state changes in the bloc but if the parent of the BlocBuilder rebuilds, then BlocBuilder will also be rebuilt. In general buildWhen is just for optimizations but it is not guaranteed to prevent rebuilds due to Flutter. Hope that helps! If you're still having trouble, I would recommend creating a sample app which illustrates the issue and sharing a link so that I can take a look.

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

All 3 comments

Hi @ThalapathySiva 馃憢
Thanks for opening an issue!

buildWhen only works to prevent rebuilds triggered by state changes in the bloc but if the parent of the BlocBuilder rebuilds, then BlocBuilder will also be rebuilt. In general buildWhen is just for optimizations but it is not guaranteed to prevent rebuilds due to Flutter. Hope that helps! If you're still having trouble, I would recommend creating a sample app which illustrates the issue and sharing a link so that I can take a look.

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

@felangel thanks for your response... Now i can understand that, and I have a doubt too, so considering the above builder which will have a parent builder, so inside the parent builder will have lot of builder widgets so my question is how can we prevent the particular builder rebuilds even though parent builder rebuild. or else what's is the way to give a condition to render particular widget in a screen when yeild a state. thanks in advance

Hi @ThalapathySiva 馃憢

You can't prevent framework rebuilds and you shouldn't be preoccupied by that since that's how flutter works. Having multiple rebuilds is something common in flutter, so trying to over optimize is just fighting against the framework.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rsnider19 picture rsnider19  路  3Comments

shawnchan2014 picture shawnchan2014  路  3Comments

nerder picture nerder  路  3Comments

clicksocial picture clicksocial  路  3Comments

tigranhov picture tigranhov  路  3Comments