目錄

unawaited_futures

async 函式主體中的 Future 結果必須使用 dart:async 進行 await 或標記為 unawaited

此規則自 Dart 2.0 起可用。

此規則有快速修復可用。

詳細資訊

#

應該在 async 函式主體內等待返回 Future 的函式。

在 async 方法中很容易忘記 await,因為命名慣例通常不會告訴我們方法是同步還是非同步(dart:io 中的某些方法除外)。

當您真的想啟動一個「啟動後不理」的 Future 時,建議的方式是使用來自 dart:asyncunawaited// ignore// ignore_for_file 註解也有效。

錯誤範例

dart
void main() async {
  doSomething(); // Likely a bug.
}

正確範例

dart
Future doSomething() => ...;

void main() async {
  await doSomething();

  unawaited(doSomething()); // Explicitly-ignored fire-and-forget.
}

用法

#

要啟用 unawaited_futures 規則,請在您的analysis_options.yaml 檔案中的 linter > rules 下新增 unawaited_futures

analysis_options.yaml
yaml
linter:
  rules:
    - unawaited_futures