unawaited_ futures
Learn about the unawaited_futures linter rule.
Future results in async function
bodies must be awaited or marked
unawaited using dart:async.
Details
#
DO await functions that return a
Future inside of an async function body.
It's easy to forget await in async methods as naming
conventions usually don't tell us if a method is sync or async
(except for some in dart:io).
When you really do want to start a fire-and-forget
Future, the recommended way is to use
unawaited from dart:async. The
// ignore
and
// ignore_for_file comments also work.
BAD:
void main() async {
doSomething(); // Likely a bug.
}
GOOD:
Future doSomething() => ...;
void main() async {
await doSomething();
unawaited(doSomething()); // Explicitly-ignored fire-and-forget.
}
Enable
#
To enable the unawaited_futures rule, add
unawaited_futures under
linter > rules in your
analysis_options.yaml
file:
linter:
rules:
- unawaited_futures
If you're instead using the YAML map syntax to configure
linter rules, add unawaited_futures: true under
linter > rules:
linter:
rules:
unawaited_futures: true
除非另有说明,文档之所提及适用于 Dart 3.12.2 版本报告页面问题.