unawaited_futures
Future
results in async
function bodies must be await
ed or marked unawaited
using dart:async
.
此规则自 Dart 2.0 版本起可用。
此规则提供 快速修复 。
详情
#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.
}
使用方法
#要启用 unawaited_futures
规则,请在你的 analysis_options.yaml
文件中,在 linter > rules 下添加 unawaited_futures
:
linter:
rules:
- unawaited_futures
除非另有说明,否则本网站上的文档反映的是 Dart 3.6.0。页面最后更新于 2025-02-05。 查看源代码 或 报告问题.