英文:
Dart Future.delayed() without anonymous function with return value
问题
以下是您要翻译的内容:
"Is it possible to call Future.delayed()
without using an anonymous function, and where the function returns a value? For example, here's Future.delayed()
that doesn't use an anonymous function, but the function doesn't return a value:
void myFunc() {
print("Hello from myFunc()");
}
void main() {
Future.delayed(Duration(seconds: 1), myFunc);
}
How would I use Future.delayed()
to call something like and get/use the return value:
int myFunc() {
return 123;
}
"
英文:
Is it possible to call Future.delayed()
without using an anonymous function, and where the function returns a value? For example, here's Future.delayed()
that doesn't use an anonymous function, but the function doesn't return a value:
void myFunc() {
print("Hello from myFunc()");
}
void main() {
Future.delayed(Duration(seconds: 1), myFunc);
}
How would I used Future.delayed()
to call something like and get/use the return value:
int myFunc() {
return 123;
}
?
答案1
得分: 1
Future.delayed 接受一个在延迟之后运行的计算。该计算的结果将被返回。因此,您可以这样做:
int myFunc() {
return 123;
}
void main() async {
final result = await Future.delayed(Duration(seconds: 1), myFunc);
print(result);
}
英文:
Future.delayed takes a computation that will run after the delay. The result of this computation will be returned. So you can do this:
int myFunc() {
return 123;
}
void main() async {
final result = await Future.delayed(Duration(seconds: 1), myFunc);
print(result);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论