is there any way to cancel a dart Future?

后端 未结 9 1513
礼貌的吻别
礼貌的吻别 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:21

    my 2 cents worth...

    class CancelableFuture {
      bool cancelled = false;
      CancelableFuture(Duration duration, void Function() callback) {
        Future.delayed(duration, () {
          if (!cancelled) {
            callback();
          }
        });
      }
    
      void cancel() {
        cancelled = true;
      }
    }
    

提交回复
热议问题