【问题标题】:Dart keyword 'await' not recognized in Android StudioAndroid Studio 中无法识别 Dart 关键字“await”
【发布时间】:2019-05-24 08:31:01
【问题描述】:

考虑以下 Dart 源代码(Flutter 项目):

void foo() {
  await bar();
}

Future<void> bar() async {
  print("test");
}

这导致 Android Studio 出现以下错误: The built-in identifier 'await' can't be used as a type.

如果我将其更改为 (await bar());,则会收到错误 Unexpected text 'await'

我在这里错过了什么?

【问题讨论】:

  • 它必须在异步函数中使用

标签: android-studio dart flutter


【解决方案1】:

foo()方法需要标记async

Future<void> foo() async {
  await bar();
}

【讨论】:

  • 这是正确的,谢谢。在这之后我感觉很愚蠢,所以我重新阅读了the docs,确实这个“细节”并没有立即清楚。我把它误解为asyncMethod().then(...) 的简写。
猜你喜欢
  • 2020-07-09
  • 2015-06-08
  • 2020-12-03
  • 2020-10-09
  • 1970-01-01
  • 2018-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多