is there any way to cancel a dart Future?

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

    How to cancel Future.delayed

    A simple way is to use Timer instead :)

    Timer _timer;
    
    void _schedule() {
      _timer = Timer(Duration(seconds: 2), () { 
        print('Do something after delay');
      });
    }
    
    @override
    void dispose() {
      super.dispose();
      _timer?.cancel();
    }
    

提交回复
热议问题