【发布时间】:2021-10-07 07:01:51
【问题描述】:
我正在尝试创建一个处理常见事件的CustomBloc。 CustomBloc 被许多其他集团扩展以添加功能。
我的问题是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
请注意,如果我不在HomeBloc 中覆盖mapEventToState,一切正常。
有什么想法吗?
【问题讨论】:
标签: flutter-bloc