Child bloc calling its ancestor mapEventToState

Issue

I’m trying to create a CustomBloc that handles common events. CustomBloc is extended by many other Blocs to add functionality.
My problem is the call of super.mapEventToState in CustomBloc descendants never gets executed.

class CustomBloc extends Bloc<BlocEvent, BlocState> {
  CustomBloc(BlocState initialState) : super(initialState) 
  @override
  Stream<BlocState> mapEventToState(BlocEvent event) async* {
    if (event is BlocInitialEvent) {
      yield BlocInitialState(BlocStatus.blocBusy)
      try {
         //do something
        yield BlocInitialState(BlocStatus.blocReady);
      } catch (e) {
        yield BlocInitialState(BlocStatus.blocError);
      }
    }
  }
}

Now let’s subclass our custom bloc:

class HomeBloc extends CustomBloc {
  HomeBloc(BlocState initialState) : super(initialState);
 
  @override
  Stream<BlocState> mapEventToState(BlocEvent event) async* {
    super.mapEventToState(event);// the event is never dispatched to CustomBloc
  //Handle more events
  }
}

HomeBloc(BlocInitialState(BlocStatus.blocReady)).Add(BlocInitialEvent);// BlocInitialEvent never gets dispatched to the ancestor class

Note that if I don’t override mapEventToState in HomeBloc everything works fine.
Any ideas?

Solution

You need to yield states from super.mapEventToState(event);

So try calling yield* super.mapEventToState(event);

Answered By – Rahul

Answer Checked By – David Marino (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *