作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
是否有任何优雅的方法可以直接在 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。代码会更干净。
关于flutter - 将 mapEventToState 与传入流一起使用的最佳实践?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59049525/
是否有任何优雅的方法可以直接在 mapEventToState() 内部映射来自私有(private) api 的传入流,而无需在 bloc 中创建冗余的私有(private)事件? 我带来了这个解决
我有 Courses和 Tasks .每个Course有很多Tasks .这就是为什么我在应用程序中使用不同的屏幕来显示类(class)列表,点击类(class)后,我将导航到下一个屏幕 - 任务列表
我正在尝试制作一个简单的应用程序来测试 BloC 模式,但是 BloC 模式的当前版本发生了变化,“当前状态”现在不使用 mapEventToState 方法作为参数发送,而是根据在线文档,您可以仅将
我是一名优秀的程序员,十分优秀!