【发布时间】:2020-03-21 18:56:25
【问题描述】:
是否有任何优雅的方法可以直接在 mapEventToState() 内部映射来自私有 api 的传入流,而无需在 bloc 中创建冗余私有事件?
我带来了这个解决方案。单个流没问题,但是多个流开始变得一团糟。提前致谢。
// (don't mind the imports, this is the bloc file)
class ExampleBloc extends Bloc<ExampleEvent, ExampleState> {
final MyPrivateApi api = MyPrivateApi.instance; // singleton
ExampleBloc() {
// api has a stream of booleans
api.myStream.listen((b) {
// if it's true fire this event
if (b) this.add(_MyPrivateEvent());
}
@override
ExampleState get initialState => InitialExampleState();
@override
Stream<ExampleState> mapEventToState(
ExampleEvent event,
) async* {
if (event is _MyPrivateEvent) {
yield SomeState;
}
}
// private Event
class _MyPrivateEvent extends ExampleEvent {
}
【问题讨论】:
标签: flutter design-patterns bloc