调用其祖先 mapEventToState 的子块
Child bloc calling its ancestor mapEventToState
我正在尝试创建一个 CustomBloc
来处理常见事件。 CustomBloc
由许多其他 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
请注意,如果我不覆盖 HomeBloc
中的 mapEventToState
,一切正常。
有什么想法吗?
您需要从 super.mapEventToState(event);
产生状态
所以尝试调用 yield* super.mapEventToState(event);
我正在尝试创建一个 CustomBloc
来处理常见事件。 CustomBloc
由许多其他 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
请注意,如果我不覆盖 HomeBloc
中的 mapEventToState
,一切正常。
有什么想法吗?
您需要从 super.mapEventToState(event);
所以尝试调用 yield* super.mapEventToState(event);