【问题标题】:Child bloc calling its ancestor mapEventToState子块调用其祖先 mapEventToState
【发布时间】:2021-10-07 07:01:51
【问题描述】:

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

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

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

【问题讨论】:

    标签: flutter-bloc


    【解决方案1】:

    您需要从super.mapEventToState(event); 产生状态

    所以尝试拨打yield* super.mapEventToState(event);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-29
      • 2011-07-23
      • 1970-01-01
      • 2015-02-16
      • 1970-01-01
      • 1970-01-01
      • 2015-04-15
      • 2017-11-25
      相关资源
      最近更新 更多