【发布时间】:2019-11-26 13:07:22
【问题描述】:
在询问了 Flutter (Difference between ChangeNotifierProvider and ScopedModel in Flutter) 中的状态管理后没有任何答案,我决定使用 BLoC Package,这在我看来是最清晰和最容易使用的。 现在在我的颤振应用程序中,我有 BlocA、BlocB 和 BlocC,我想从 BlocC 监听 BlocA 和 BlocB 的状态变化。
在所有区块上具有相同状态/事件的示例(更新、更新/更新):
class BlocC extends Bloc<CEvent, CState> {
final BlocA a;
final BlocB b;
StreamSubscription aSubscription;
BlocC({@required this.a, @required this.b}) {
aSubscription = a.state.listen((state) {
if (state is AUpdated) {
dispatch(UpdateC());
}
});
}
@override
CState get initialState => CUpdating();
@override
Stream<CState> mapEventToState(CEvent event) async* {
if (event is UpdateC && b.currentState is BUpdated) {
yield* CUpdated();
}
}
...
在这种情况下,当从 BlocA 状态分派事件时,BlocB 的状态有时不会在 _mapEventToState 方法中更新,并且我的侦听器不起作用。 所以我认为有一种方法可以将一个块订阅到许多流,以获得所有流的正确状态转换。
你能帮帮我吗?
【问题讨论】: