Dart中的Future.delayed()没有匿名函数并且有返回值。

huangapple go评论63阅读模式
英文:

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);
}

huangapple
  • 本文由 发表于 2023年4月20日 08:30:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76059732.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定