0

I am a beginner in flutter and is there any examples or documentations which related to multi blocbuilder in one page by using flutter_bloc package in Flutter.

SOUGAT RIYADH
  • 562
  • 2
  • 10
  • 26
  • I prefer using bloc_pattern with rxdart and streambuilder. So I can have as many blocs as I want in my UI doing something like this: "final bloc = BlocProvider.getBloc();"... Maybe you could read about it to see which one you like most: https://pub.dev/packages/bloc_pattern – Stel Jan 02 '20 at 12:05
  • @Stel In bloc_pattern is hard to understand the code so that's why i prefer flutter_bloc. – SOUGAT RIYADH Jan 02 '20 at 13:28

2 Answers2

7

The latest flutter_bloc docs explain how to create a MultiBlocBuilder, when your Widget's builder relies on multiple BLOC's:

Builder(
  builder: (context) {
    final stateA = context.watch<BlocA>().state;
    final stateB = context.watch<BlocB>().state;
    final stateC = context.watch<BlocC>().state;

    // return a Widget which depends on the state of BlocA, BlocB, and BlocC
  }
);

The flutter_multi_bloc_builder somewhat simplifies this syntax (... does it?), its latest doc also describes how to do the same thing using that package:

final bloc1 = BlocProvider.of<MyBloc1>(context);
final bloc2 = BlocProvider.of<MyBloc2>(context);
final bloc3 = BlocProvider.of<MyBloc2>(context);

MultiBlocBuilder(
    blocs: [bloc1, bloc2, bloc3],
    builder: (context, states) {
        final state1 = states.get<MyBloc1State>();
        final state2 = states.get<MyBloc2State>();
        final state3 = states.get<MyBloc3State>();
        
        if (state1 is Loading || state2 is Loading || state3 is Loading) {
            return Text("Loading");
        } else {
            return Text("SHow some content");
        }
    }
);

Might have been more useful if some buildWhen or selector equivalent had been offered in this package?

I hope this answers the question.

Will59
  • 1,430
  • 1
  • 16
  • 37
0

BlocBuilder(), like any other Widgets, can be used multiple times. To use multiple of BlocBuilders in a screen, you just need to provide the needed Streamable (Bloc) on different BlocBuilder.

Omatt
  • 8,564
  • 2
  • 42
  • 144