gpt4 book ai didi

flutter_bloc 8 : what is the best practice for listening to state changes from another bloc

转载 作者:行者123 更新时间:2023-12-03 08:06:47 25 4
gpt4 key购买 nike

倾听另一个 Bloc 状态变化的最佳实践是什么?

这个答案在以前的版本中是相关的,但在版本 8 中不起作用(.listen 方法在 block 上不再存在):https://stackoverflow.com/a/62785980/160919

FilteredTodosBloc({@required this.todosBloc}) {
todosSubscription = todosBloc.listen((state) {
if (state is TodosLoadSuccess) {
add(TodosUpdated((todosBloc.state as TodosLoadSuccess).todos));
}
});}

在 flutter_bloc 8 中监听另一个 block 的状态变化的推荐方法是什么?

最佳答案

状态流现在通过 stream getter 公开,因此您仍然可以使用几乎相同的代码:

FilteredTodosBloc({required this.todosBloc}) {
todosSubscription = todosBloc.stream.listen((state) {
if (state is TodosLoadSuccess) {
add(TodosUpdated((todosBloc.state as TodosLoadSuccess).todos));
}
});}

关于flutter_bloc 8 : what is the best practice for listening to state changes from another bloc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72085170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com