unawaited_futures
async 函数体中的 Future 结果必须使用 await 等待或使用 dart:async 中的 unawaited 标记。
详情
#请务必在 async 函数体中等待返回 Future 的函数。
在 async 方法中很容易忘记使用 await,因为命名约定通常不会告诉我们一个方法是同步还是异步的(dart:io 中的一些方法除外)。
当你确实想启动一个“即发即忘”的 Future 时,推荐的方法是使用 dart:async 中的 unawaited。// 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如果你使用 YAML map 语法来配置 linter 规则,请在 linter > rules 下添加 unawaited_futures: true
analysis_options.yaml
yaml
linter:
rules:
unawaited_futures: true