Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 为什么';在onPressed函数中不需要使用wait关键字吗?_Flutter_Dart - Fatal编程技术网

Flutter 为什么';在onPressed函数中不需要使用wait关键字吗?

Flutter 为什么';在onPressed函数中不需要使用wait关键字吗?,flutter,dart,Flutter,Dart,为什么在onPressed函数中不需要使用wait关键字?例如,有一个url_启动器插件。用法如下: onPressed: _launchURL, ... _launchURL() async { const url = 'https://flutter.dev'; if (await canLaunch(url)) { await launch(url); } else { throw 'Could not launch $url'; } } 在dart中,在

为什么在onPressed函数中不需要使用wait关键字?例如,有一个url_启动器插件。用法如下:

onPressed: _launchURL,
...
_launchURL() async {
  const url = 'https://flutter.dev';
  if (await canLaunch(url)) {
    await launch(url);
  } else {
    throw 'Could not launch $url';
  }
}

在dart中,在调用异步函数之前是否需要使用wait关键字?那么为什么我们不在按下
中的
\u launchURL
之前使用wait关键字呢?

就像在评论中所说的那样,您没有调用函数,因此没有什么需要等待的。 然而,
onPressed
实际上期望任何带有
void
返回类型的函数,这意味着它不会等待您提供给它的任何函数


因此,基本上onPressed只是启动未来,然后忘记它

,因为这里没有定义函数,也没有执行它,只是将它分配给某个属性,然后有人会等待onPressed()