unawaited_ futures
Future
results in
async
function bodies must be
await
ed 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
Unless stated otherwise, the documentation on this site reflects Dart 3.9.2. Report an issue.