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
} 相关推荐
万物weiyi 2020-06-16
Samlss 2020-06-04
liutong 2020-05-12
万物weiyi 2020-03-04
mryangjx 2020-03-01
liutong 2020-02-26
Samlss 2020-02-22
chaoxiao 2020-02-15
mryangjx 2020-01-28
mryangjx 2020-01-25
liutong 2020-01-12
apowerfulman 2020-01-07
Samlss 2020-01-07
万物weiyi 2020-01-01
mryangjx 2019-12-25
Samlss 2019-12-15
Dreamfine 2019-12-13