Bloc: MultiBlocBuilder?

Created on 3 Feb 2020  路  6Comments  路  Source: felangel/bloc

A case of use when this feature is helpful is when we have a widget that responses at two or more independents external events. In this case, the widget is wrapped with two or more bloc builder that managed this events. Nested blocs can't be a really problem, but is expressive to write, like that as create another bloc which has dependencies on the two blocs.

A example where this feature can be helpful.

return BlocBuilder<BlocA, bool>(
  builder: (context, stateA) {
    return BlocBuilder<BlocB, int>(
      builder: (context, stateB) {
        if (stateA || stateB == 1) {
          // do something
        }
        // do another thing
      }
    );
  },
);

Some help?

Thanks for this unbelievable lib!!!

duplicate

Most helpful comment

I really don't want to introduce functionality in the library that isn't type-safe. If you'd like I can put together an implementation that you can use within your own project but I would prefer not to include that in the library. If you don't mind can you share your use case? There might be a better way to approach the issue which doesn't rely on many nested BlocBuilders.

All 6 comments

Hi @EdsonOnildoJR 馃憢
Thanks for opening an issue and for the positive feedback!

This is a duplicate of https://github.com/felangel/bloc/issues/538 and we decided against it because in order for it to work we'd need MultiBlocBuilder2, MultiBlocBuilder3, ..., MultiBlocBuilderN and the API would be very verbose to the point where it's actually more readable to just nest BlocBuilders. Another option is to create another bloc which composes the states of the two blocs so you have just a single BlocBuilder which your UI responds to. Let me know what you think.

I don't fluent in dart, something like that it's possible?

MultiBlocBuilder(
  blocs: [Bloc1, Bloc2, BlocN],
  states: [State1, State2, StateN],
  builder: (context, stateA, stateB, stateN) {
    // ...
  }
);

I'm thinking in this widget just returns a nested BlocBuilder.

Thanks for the fast answer and feedback!!

@EdsonOnildoJR what you're suggesting would only be possible if the states were dynamically typed. You would then have to cast your states like:

MultiBlocBuilder(
  blocs: [Bloc1, Bloc2, BlocN],
  states: [State1, State2, StateN],
  builder: (context, stateA, stateB, stateN) {
    // stateA, stateB, stateN are dynamic at this point.
    if ((stateA as StateA) is InitialStateA) { ... }
  }
);

It's sounds good for me! Better that another solutions available in this moment (nested BlocBuilder, etc...)

I can't think anything better, maybe pass the states through the generics, but that would limit the number of blocs...

What would you think about that?

I really don't want to introduce functionality in the library that isn't type-safe. If you'd like I can put together an implementation that you can use within your own project but I would prefer not to include that in the library. If you don't mind can you share your use case? There might be a better way to approach the issue which doesn't rely on many nested BlocBuilders.

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

timtraversy picture timtraversy  路  3Comments

craiglabenz picture craiglabenz  路  3Comments

rsnider19 picture rsnider19  路  3Comments

ricktotec picture ricktotec  路  3Comments

krusek picture krusek  路  3Comments