unawaited_futures
Future
results in async
function bodies must be await
ed or marked unawaited
using dart:async
.
This rule is available as of Dart 2.0.
This rule has a quick fix available.
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.
}
Usage
#To enable the unawaited_futures
rule,
add unawaited_futures
under linter > rules in your
analysis_options.yaml
file:
linter:
rules:
- unawaited_futures
除非另有说明,文档之所提及适用于 Dart 3.5.4 版本,本页面最后更新时间: 2024-08-02。 查看文档源码 或者 报告页面问题。