子块调用其祖先 mapEventToState

时间:2021-08-01 17:35:29

标签: flutter-bloc

我正在尝试创建一个处理常见事件的 CustomBlocCustomBloc 由许多其他 Bloc 扩展以添加功能。 我的问题是 super.mapEventToState 后代中 CustomBloc 的调用永远不会被执行。

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);
      }
    }
  }
}

现在让我们对自定义块进行子类化:

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

请注意,如果我不在 mapEventToState 中覆盖 HomeBloc,一切正常。 有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您需要从 super.mapEventToState(event);

产生状态

所以尝试调用 yield* super.mapEventToState(event);