假设我想创建一个异步方法。我可以将其返回类型设置为 Future void 或简单地“void”(如下面的示例所示)。两种方法似乎都能奏效。那么两者有什么区别呢?什么时候应该使用 Future void 而不是 void?谢谢!
Future<void> myMethod() async{
await myOtherMethod(); //myOtherMethod() has return type of Future<void>
print('something');
}
Run Code Online (Sandbox Code Playgroud)
与
void myMethod() async{
await myOtherMethod(); //myOtherMethod() has return type of Future<void>
print('something');
}
Run Code Online (Sandbox Code Playgroud)
hol*_*ola 10
Future<void>在您想要的地方使用
调用未来的函数:
myMethod().then((_) => ...).catchError((err) => ...);
Run Code Online (Sandbox Code Playgroud)
用于await更具可读性的异步代码。
await myMethod();
await anotherMethod();
Run Code Online (Sandbox Code Playgroud)
void在您想要射击并忘记的地方使用。
myMethod();
... do other stuff
Run Code Online (Sandbox Code Playgroud)
这Future<void>是更常见的。如果您不确定使用哪一个,请使用Future<void>.
| 归档时间: |
|
| 查看次数: |
3067 次 |
| 最近记录: |