[英]Dart/Flutter - yield from callback function
I need to make a function call which does not return anything ( void
).我需要进行一个不返回任何内容的函数调用(
void
)。 Only way to get notified about function completion is sending a callback
function.获得有关函数完成通知的唯一方法是发送
callback
函数。
Now I an using BLoC
pattern along with ReDux
, When an event is dispatched, I dispatch another action to store of redux
, After action
is completed it calls the callback
function.现在我将
BLoC
模式与ReDux
一起使用,当一个事件被调度时,我将另一个动作调度到redux
存储中, action
完成后它调用callback
函数。 And now inside callback
function, I want to update the state
of bloc
.现在在
callback
函数中,我想更新bloc
的state
。 Below is my implementation,下面是我的实现,
if (event is Login) {
yield currentState.copyWith(formProcessing: true);
store.dispatch(authActions.login(
currentState.username,
currentState.password,
(error, data) {
print(error);
print(data);
// I want to yield here.
yield currentState.copyWith(formProcessing: false);
},
));
}
As shown in the above code snippet, Inside the callback function, I want to yield
.如上面的代码片段所示,在回调函数中,我想要
yield
。
Create a function that returns the future and makes callback function to store dispatch, Heres sample.创建一个返回未来的函数并制作回调函数来存储调度,这里是示例。
if (event is Login) {
yield currentState.copyWith(formProcessing: true);
try {
dynamic result = await loginAction(store, currentState.username, currentState.password);
print(result);
yield currentState.copyWith(formProcessing: false);
} catch (e) {
print(e);
}
}
Future loginAction(store, username, password) {
var completer = new Completer();
store.dispatch(authActions.login(
username,
password,
(error, data) {
if (error != null) {
completer.completeError(error);
} else if (data != null) {
completer.complete(data);
}
},
));
return completer.future;
}
You need to create other event
, and dispatch
this event
in your callback
function, then you can do what you want in the function that filter your events
.您需要创建其他
event
,并在您的callback
函数中dispatch
此event
,然后您可以在过滤events
的函数中执行您想要的操作。
I don't know what is the purpose of your BLoC, but the name of this event
depends on the use case, it can be UpdateForm
, UpdateState
, LoggedIn
, LoggedOut
, etc. You will find the most descriptive name for your use case.我不知道您的 BLoC 的目的是什么,但此
event
的名称取决于用例,可以是UpdateForm
、 UpdateState
、 LoggedIn
、 LoggedOut
等。您会找到最适合您的用例的描述性名称。
Remember that you can also create this event
with parameters, for example UpdateForm (bool isLoggedIn)
, and yield
different states
according to your conditions.请记住,您还可以使用参数创建此
event
,例如UpdateForm (bool isLoggedIn)
,并根据您的条件yield
不同的states
。
For the example, the name of this event
is OtherEvent
.例如,此
event
的名称是OtherEvent
。
if (event is Login) {
yield currentState.copyWith(formProcessing: true);
store.dispatch(authActions.login(
currentState.username,
currentState.password,
(error, data) {
print(error);
print(data);
dispatch(OtherEvent());
},
));
} else if (event is OtherEvent) {
// You can yield here what you want
yield currentState.copyWith(formProcessing: false);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.