Skip to content

null_into_non_nullable_future

Reports a value that may be null given to Future<T>.value or Completer<T>.complete where T cannot be null. Give a value that is never null.

Tier
advisory
Scope
Dart
Lowest preset
recommended
Fix
none

A value that may be null given to Future<T>.value, or to complete of a Completer<T>, whose T cannot be null: Future<int>.value(cached) with cached an int?. Both take a nullable argument, so the compiler lets the value through, and a null fails at run time with a type error where the future is awaited. T is the type written or inferred. The violation covers the value.

It does not report:

  • a T that may be nullable: int?, void, a type parameter;
  • a value of type dynamic;
  • a missing value or a literal null, which the analyzer’s own warning reports.

The type error appears in the code that awaits the future, often in another layer, and names Null and FutureOr<int> rather than the line that gave the value. A cache that is empty only on the first run makes it rare and late.

Violating
Future<int> count() => Future<int>.value(_cached);
Corrected
Future<int> count() => Future<int>.value(_cached ?? 0);

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 caller checks the value for null before.
// ignore: null_into_non_nullable_future
Future<int>.value(cached);

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