Skip to content

nested_future

Reports a written type of a future of a future, such as Future<Future<int>>. Return or await the inner future instead.

Tier
advisory
Scope
Dart
Lowest preset
strict
Fix
none

A Future<Future<…>>, or a FutureOr of a Future, nullable or not, wherever a type is written: a return type, a parameter, a variable or a type argument. A type nested deeper is reported once, at the outermost future. A type alias that hides the nesting is reported only where its target is written as a future.

It does not report:

  • an inferred type;
  • a future of another type that holds futures, such as Future<List<Future<int>>>;
  • a stream of futures.

One await gives the inner future, not its value. A caller that awaits once believes the work is done when it has only started, and no one catches the inner future’s failure.

Violating
Future<Future<User>> start() async => api.fetchUser();
Corrected
Future<User> start() => api.fetchUser();

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:

// The scheduler gives the job, which starts when it is awaited.
// ignore: nested_future
Future<Future<int>>? job;

// ignore_for_file: assay/nested_future silences it in a whole file, and the prefixed // ignore: assay/nested_future works too. See Suppressions.