是否有任何优雅的方法可以直接在 mapEventToState() 内部映射来自私有(private) api 的传入流,而无需在 bloc 中创建冗余的私有(private)事件?
我带来了这个解决方案。一个流没问题,但多个流开始变得一团糟。提前致谢。
// (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 {
}
最佳答案
如我所见,您可以在屏幕上订阅事件更新,如果需要一些计算,可以将事件从屏幕推送到 Bloc。代码会更干净。
https://stackoverflow.com/questions/59049525/
相关文章:
angular - ViewChild 与 Directive 获取 ViewContainerRe
git - 在 CLion 远程工具链中跟踪 git 提交时如何触发 cmake 重新配置
linkedin - 检索 Linkedin Video Post 的缩略图 (ugcPost AP
angular - 如果我重新分配一个存储 rxjs 订阅的变量,那会取消订阅吗?
php - 使用 XDebug、VSCode 和 Docker 进行调试
android - 无法再在 Android Studio 中运行单独的插桩测试
python-3.x - 如何在 Python 的 Microsoft O365 日历中将日期作为查
hibernate - Spring JPA 审计因多个数据源而失败