我正在尝试创建一个 BLOC,它依赖于另外两个基于时间的块和一个非基于时间的块。我的意思是基于时间,例如他们正在连接远程服务器,因此需要时间。它的工作原理如下:
登录(当然需要一些时间)
如果登录成功
做另一个过程(这也需要时间。它返回一个未来。)
登录且另一个进程完成后,让页面知道这一点。
我的 BLOC 取决于这三个:
final UserBloc _userBloc;
final AnotherBloc _anotherBloc;
final FinishBloc _finishBloc;
在映射事件到状态方法中,我应该调度相关事件。但是我不能等待它们是否完成。
_userBloc.dispatch(
Login(),
);
_anotherBloc.dispatch(
AnotherProcess(),
);
//LetThePageKnowIt should work after login and another process
_finishBloc.dispatch(
LetThePageKnowIt(),
);
在发送某些东西之前有没有一种干净的方法来等待其他人?
我知道我使用了一种我不喜欢的方式。在我连接其中其他块的主块状态中,我有布尔值。
class CombinerState {
bool isLoginFinished = false;
bool isAnotherProcessFinished = false;
我正在主块的构造函数中监听时间相关块的状态。当它们产生“我完成了”时,我只是将布尔值标记为“true”。
MainBloc(
this._userBloc,
this._anotherBloc,
this._pageBloc,
); {
_userBloc.state.listen(
(state) {
if (state.status == Status.finished) {
dispatch(FinishLogin());
}
},
);
_anotherBloc.state.listen(
(state) {
if (state.status == AnotherStatus.finished) {
dispatch(FinishAnotherProcess());
}
},
);
}
我为 main bloc 调度另一个事件,以在将 bool 设置为 true 后检查所有 bool 是否都为 true。
else if (event is FinishAnotherProcess) {
newState.isAnotherProcessFinished = true;
yield newState;
dispatch(CheckIfReady());
}
如果布尔值为真,我调度 LetThePageKnowIt()
else if (event is CheckIfReady) {
if (currentState.isAnotherProcessFinished == true &&
currentState.isLoginFinished == true) {
_pageBloc.dispatch(LetThePageKnowIt());
}
}
我对这段代码不满意。我正在寻找一种方法来等待其他集团发送“完成”状态。之后我想发送我的 LetThePageKnowIt()