Bloc: Combining 2 Bloc state in a new Bloc

Created on 25 Jun 2019  路  6Comments  路  Source: felangel/bloc

Hello,

what is best practice for combing multiple Bloc state in a new Bloc? For example I have BlocA and BlocB and I want to create a new Bloc which combines both states:

class BlocCombineBloc extends Bloc<BlocCombineEvent, BlocCombineState> {
  final BlocABloc blocA;
  final BlocBBloc blocB;

  BlocCombineBloc(this.blocA, this.blocB);

  @override
  BlocCombineState get initialState => InitialBlocCombineState();

  @override
  Stream<BlocCombineState> mapEventToState(
    BlocCombineEvent event,
  ) async* {
    Observable.combineLatest2(blocA.state, blocB.state,
        (BlocAState stateA, BlocBState stateB) {
      if (stateA is BlocALoaded && stateB is BlocBLoaded) {
        /// combine data
      }
    }).listen((data) {
      /// do something with data
    });
  }
}

The problem which I am facing is when my BlocCombineBloc calls disposed(), BlocA and BlocB call also disposed().

bug

Most helpful comment

Hey @anatter 馃憢
Thanks for opening an issue and for the PR!

@filoe thanks for catching that! I must鈥檝e missed it during the recent refactor. Will merge the PR shortly and include the fix in flutter_bloc v0.18.3.

Thanks again everyone! 馃檹

All 6 comments

I have the same case but fix it from other side. With the following CombineLatestBlocBuilder

I made a sample application with my problem: https://github.com/anatter/flutter_bloc_dispose
I have no Idea why BlocA and BlocB call dispose...

Don't really know the package but I would guess the dispose parameter is missing here:
https://github.com/felangel/bloc/blob/master/packages/flutter_bloc/lib/src/bloc_provider.dart#L72
Can't really tell whether there is any reason to exclude the dispose parameter. If so, there has to be an alternate solution.
What would u guess @felangel

Don't really know the package but I would guess the dispose parameter is missing here:
https://github.com/felangel/bloc/blob/master/packages/flutter_bloc/lib/src/bloc_provider.dart#L72
Can't really tell whether there is any reason to exclude the dispose parameter. If so, there has to be an alternate solution.
What would u guess @felangel

Thanks @filoe, this resolved the issue :laughing: :tada:

@felangel I opened a Pull request: #377

Hey @anatter 馃憢
Thanks for opening an issue and for the PR!

@filoe thanks for catching that! I must鈥檝e missed it during the recent refactor. Will merge the PR shortly and include the fix in flutter_bloc v0.18.3.

Thanks again everyone! 馃檹

Fix merged in #377 and included in flutter_bloc v0.18.3 馃帀
Thanks everyone! 馃挴

Was this page helpful?
0 / 5 - 0 ratings

Related issues

craiglabenz picture craiglabenz  路  3Comments

komapeb picture komapeb  路  3Comments

timtraversy picture timtraversy  路  3Comments

RobPFarley picture RobPFarley  路  3Comments

shawnchan2014 picture shawnchan2014  路  3Comments