Skip to content

future_awaited_too_late

Reports a future started into a local variable and awaited only after the function waited for something else. Await the futures together, or await this one first.

Tier
blocking
Scope
Dart
Lowest preset
recommended
Fix
none

A future started into a local variable of an async function and awaited only after the function waited for something else. The violation is on the name of the variable.

  • The variable’s value is a call or a creation whose static type is a Future of dart:async.
  • The first place after the declaration that names the variable is the operand of an await, parentheses allowed.
  • Before it, an await or an await for of the function itself, not of a closure in it, takes effect, also in a branch or a loop.

It does not report:

  • a future already held elsewhere: final f = _loading;, a parameter;
  • Future.value and Flutter’s SynchronousFuture, which cannot fail;
  • a future given a handler where it is made: catchError, onError, ignore, a then with onError;
  • a first use other than an await: Future.wait([a, b]), (a, b).wait, unawaited(a), a closure that names it;
  • anything on the test side of a project: there an uncaught error fails the test that started the future, as the later await would, and tests start work on purpose before waiting for something else.

A then without onError and a whenComplete give the future no handler: what they make still fails, so it is reported.

While the function waits for the other future, nothing listens to the first. If it fails in that time, its error goes to the zone as an uncaught error (a probe on the VM showed it). That ends a Dart program and is reported as a crash in Flutter. The later await then throws it a second time, and a try around that await does not catch the first.

Awaiting the futures together keeps both started and catches both.

Violating
Future<Page> open() async {
final profile = loadProfile();
final settings = await loadSettings();
return Page(await profile, settings);
}
Corrected
Future<Page> open() async {
final (profile, settings) = await (loadProfile(), loadSettings()).wait;
return Page(profile, settings);
}

Set under the assay: key of the project’s root analysis_options.yaml.

Rule parameter Default Effect
enabled true false switches the rule off for the whole project.
exclude [] Path globs where the rule is silent.

Write why on the line directly above the suppression:

// Why it stays as written here.
// ignore: future_awaited_too_late

// ignore_for_file: assay/future_awaited_too_late silences it in a whole file, and // ignore: assay/future_awaited_too_late with the prefix works too. See Suppressions.