gpt4 book ai didi

dart - 延伸 future

转载 作者:行者123 更新时间:2023-12-03 03:13:37 26 4
gpt4 key购买 nike

我发现很多关于使用而不是关于定义 future 在 Dart 。假设我有 letsWait()这需要相当长的时间。如何使用 Future类(class)?

import 'dart:async';

void main() {
print('Let\'s get started');
ArtificialWait waitPoint = new ArtificialWait();
Future<String> future = waitPoint.letsWait();
// and how about printing the return here?
print('something fast');
}

class ArtificialWait extends Future<String> {
String letsWait() {
for (var i = 0; i < 5000000000; i++) {
// lol
}
return 'finally';
}
}

这次尝试给了我一个:
unresolved implicit call to super constructor 'Future()' class ArtificialWait extends Future<String> {

最佳答案

我不知道你为什么要从 Future 继承。
通常你会这样使用:

import 'dart:async';

void main() {
print('Let\'s get started');
artificialWait().then((e) => print(e));
// and how about printing the return here?
print('something fast');
}

Future<String> artificialWait () {
var completer = new Completer<String>();
Timer.run(() {
for (var i = 0; i < 5000000000; i++) {
// lol
}
completer.complete('finally');
});
return completer.future;
}

关于dart - 延伸 future ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20641336/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com