问题描述
我正在尝试创建一个BLOC,该BLOC依赖于其他两个基于时间的集团和一个基于非时间的集团.我所说的基于时间的意思是,例如,他们正在连接远程服务器,因此需要时间.就是这样工作的:
I am trying to create a BLOC which depends on two other time based bloc and a non-time based bloc. What i mean with time based is, for example they are connecting a remote server so it takes time. It's working just like this:
登录(这当然需要一些时间)
Login (It's of course taking some time)
如果登录成功
执行另一个过程(这也需要时间.它会返回未来.)
Do another process (This is something takes time also. It returns a future.)
登录并完成另一个过程后,让页面知道它.
After login and another process finishes, let the page know it.
我的BLOC取决于以下三个:
My BLOC depends on these three:
final UserBloc _userBloc;
final AnotherBloc _anotherBloc;
final FinishBloc _finishBloc;
在状态方法的map事件中,我应该调度相关事件.但是,我等不及他们是否完成.
Inside the map event to state method I should dispatch relevant events. However i cannot await if they are finished.
_userBloc.dispatch(
Login(),
);
_anotherBloc.dispatch(
AnotherProcess(),
);
//LetThePageKnowIt should work after login and another process
_finishBloc.dispatch(
LetThePageKnowIt(),
);
是否有一种干净的方法可以在派遣某些东西之前等待其他人?
Is there a clean way to await some others before dispatching something?
正确地知道我使用了一种我不喜欢的方式.在我连接其他集团的主要集团的状态下,我有傻瓜.
Right know I use a way that i don't like. In the main bloc's state which i connect other blocs in it, I have bools.
class CombinerState {
bool isLoginFinished = false;
bool isAnotherProcessFinished = false;
我在主块的构造函数中侦听时间相关的块状态.当他们说我完成了"时,我只是将布尔值标记为"true".
I am listening the time dependent blocs' states in constructor of main bloc. When they yield "i am finished" I just mark the bools "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());
}
},
);
}
,然后在将bool设置为true之后,为main bloc调度另一个事件,以检查所有bool是否都是true.
and I dispatch another event for main bloc to check if all the bools are true after setting a bool to true.
else if (event is FinishAnotherProcess) {
newState.isAnotherProcessFinished = true;
yield newState;
dispatch(CheckIfReady());
}
如果布尔值是正确的,我将调度LetThePageKnowIt()
If the bools are true, i dispatch LetThePageKnowIt()
else if (event is CheckIfReady) {
if (currentState.isAnotherProcessFinished == true &&
currentState.isLoginFinished == true) {
_pageBloc.dispatch(LetThePageKnowIt());
}
}
我对这段代码不满意.我正在寻找一种方法来等待其他BLOC发送完成"状态.之后,我想调度我的LetThePageKnowIt()
I am not satisfied with this code. I am looking a way to await other BLOCs send a state with "finished". After that I want to dispatch my LetThePageKnowIt()
推荐答案
@pskink的建议解决了我的问题.
@pskink 's suggestion solved my problem.
我创建了两个返回未来的方法.在他们里面,我只是在等待我的流.这是登录流的示例.
I have created two methods which return a future. Inside of them, I just await for my streams. Here is the example of login stream.
在要声明的地图事件中,在分派之后,我等待一个异步方法.
In map event to state, after the dispatches, I await an async method.
_userBloc.dispatch(
Login(),
);
_anotherBloc.dispatch(
AnotherProcess(),
);
await loginProcess();
await otherProcess();
_finishBloc.dispatch(
LetThePageKnowIt(),
);
在方法中,我只是等待userbloc完成其工作并产生收益.然后返回.
Inside the method I just await for userbloc to finish its job and yields about it. Then return.
Future loginProcess() async {
await for (var result in _userBloc.state) {
if (result.status == Status.finished) {
return;
}
}
}
这篇关于在使用flutter_bloc库调度事件之前,请等待一些结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!