Skip to content

Combining 2 Bloc state in a new Bloc #376

@anatter

Description

@anatter

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().

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions