Dart 中断Future

中断future

方法1)

import 'package:async/async.dart';

void main() {
  var get = CancelableOperation.fromFuture(
    Future.delayed(Duration(seconds: 3)),
    onCancel: () => print('onCancel'),
  );

  get.value.then(print);
  Future.delayed(Duration(seconds: 1)).then((_) => get.cancel());
}

方法2)

import 'package:async/async.dart';

void main() {
  var completer = CancelableCompleter(onCancel: () => print('onCancel'));
  completer.complete(Future.delayed(Duration(seconds: 3))); // 添加future
  completer.operation.value.then(print);// 订阅future
  Future.delayed(Duration(seconds: 1)).then((_) => completer.operation.cancel());//中断future
}
posted @ 2020-02-22 15:01  Ajanuw  阅读(592)  评论(0编辑  收藏  举报