Skip to main content

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:

dart
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:

dart
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:

dart
import 'dart:async';

Future<void> f() async {
  unawaited(g());
}

Future<int> g() => Future.value(0);