is there any way to cancel a dart Future?

后端 未结 9 1499
礼貌的吻别
礼貌的吻别 2020-11-27 05:55

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 a

9条回答
  •  情歌与酒
    2020-11-27 06:13

    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.

    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:

提交回复
热议问题