unnecessary_ async
Learn about the unnecessary_async linter rule.
No await no async.
Details
#
Functions that don't do await don't have to be
async.
Usually such functions also don't have to return a
Future, which allows an invoker to avoid
await in its code, etc. Synchronous code in
general runs faster, and is easier to reason about.
BAD:
void f() async {
// await Future.delayed(const Duration(seconds: 2));
print(0);
}
GOOD:
void f() {
// await Future.delayed(const Duration(seconds: 2));
print(0);
}
Enable
#
To enable the unnecessary_async rule, add
unnecessary_async under
linter > rules in your
analysis_options.yaml
file:
linter:
rules:
- unnecessary_async
If you're instead using the YAML map syntax to configure
linter rules, add unnecessary_async: true under
linter > rules:
linter:
rules:
unnecessary_async: true
除非另有说明,文档之所提及适用于 Dart 3.12.2 版本报告页面问题.