unnecessary_async
没有 await 就没有 async。
详情
#不执行 await 的函数不需要是 async 函数。
通常这类函数也不需要返回 Future,这样调用者可以在其代码中避免使用 await 等。同步代码通常运行更快,并且更容易理解。
反例
dart
void f() async {
// await Future.delayed(const Duration(seconds: 2));
print(0);
}正例
dart
void f() {
// await Future.delayed(const Duration(seconds: 2));
print(0);
}启用
#要启用 unnecessary_async 规则,请在您的 analysis_options.yaml 文件中,将 unnecessary_async 添加到 linter > rules 下
analysis_options.yaml
yaml
linter:
rules:
- unnecessary_async如果您使用 YAML 映射语法配置 linter 规则,请在 linter > rules 下添加 unnecessary_async: true
analysis_options.yaml
yaml
linter:
rules:
unnecessary_async: true