有没有办法取消飞镖未来? [英] is there any way to cancel a dart Future?

查看:31
本文介绍了有没有办法取消飞镖未来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Dart 用户界面中,我有一个按钮 [提交] 来启动一个长异步请求.[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.

推荐答案

据我所知,没有办法取消 Future.但是有一种方法可以取消 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.

在按钮上调用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆