有没有办法取消飞镖的未来? [英] is there any way to cancel a dart Future?
问题描述
在Dart UI中,我有一个按钮[submit]来启动长期异步请求。 [submit]处理程序返回一个Future。接下来,按钮[提交]被按钮[取消]取代,以允许取消整个操作。在[cancel]处理程序中,我想取消长操作。如何取消提交处理程序返回的Future?我发现没有办法这样做。
In a Dart UI, I have a button [submit] to launch a long async request. The [submit] handler returns a Future. Next, the button [submit] is replaced by a button [cancel] to allow the cancellation of the whole operation. In the [cancel] handler, I would like to cancel the long operation. How can I cancel the Future returned by the submit handler? I found no method to do that.
推荐答案
据我所知,没有办法取消未来。但是有一种取消Stream订阅的方法,也许可以帮助你。
As far as I know, there isn't a way to cancel a Future. But there is a way to cancel a Stream subscription, and maybe that can help you.
在a上调用 onSubmit
按钮返回 StreamSubscription
对象。您可以显式存储该对象,然后调用 cancel()
取消流式订阅:
Calling onSubmit
on a button returns a StreamSubscription
object. You can explicitly store that object and then call cancel()
on it to cancel the stream subscription:
StreamSubscription subscription = someDOMElement.onSubmit.listen((data) {
// you code here
if (someCondition == true) {
subscription.cancel();
}
});
稍后,作为对某些用户操作的响应,您可以取消订阅:
Later, as a response to some user action, perhaps, you can cancel the subscription:
这篇关于有没有办法取消飞镖的未来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!