unawaited_futures
Missing an 'await' for the 'Future' computed by this expression.
Description
#The analyzer produces this diagnostic on an expression with a Future
type, only in a few specific cases:
- when the expression is itself a statement (like
f();
), - when the expression is part of a cascade (like
C()..f()
), - when the expression is a String interpolation (like
'${f()}'
).
The analyzer only produces this diagnostic on expressions inside an
async
or async*
function.
The two common corrections are to 'await' the expression, or to wrap the
expression in a call to unawaited()
.
Example
#The following code produces this diagnostic because the function g
returns a future, but the future isn't awaited:
Future<void> f() async {
g();
}
Future<int> g() => Future.value(0);
Common fixes
#If the future needs to complete before the following code is executed,
then add an await
before the invocation:
Future<void> f() async {
await g();
}
Future<int> g() => Future.value(0);
If the future doesn't need to complete before the following code is
executed, then wrap the Future
-returning invocation in an invocation of
the unawaited
function:
import 'dart:async';
Future<void> f() async {
unawaited(g());
}
Future<int> g() => Future.value(0);
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。