Skip to content

async_callback_to_void_parameter

Reports an async closure passed where a void callback is asked for, when it can fail outside a try. Catch its failures inside the closure.

Tier
advisory
Scope
Dart
Lowest preset
strict
Fix
none

An async closure given where a function that returns void is asked for, such as Timer(delay, () async { await save(); }), when it has an await or an await for outside the body of a try with a catch clause.

It does not report:

  • a closure that catches its own failures, or has no await;
  • a closure given to the constructor of a Flutter widget, such as onPressed, or to forEach;
  • a parameter whose function returns a Future, FutureOr or dynamic.

Whoever calls a void callback does not wait for it, and does not see it fail. The failure becomes an uncaught error of the zone, and the caller goes on as if the work were done.

Listeners, timers and stream subscriptions are common places for an async closure. Every failure there goes to the zone’s handler instead of to the code that set the callback up.

The rule is in strict only because these callbacks are an idiom of Flutter code.

Violating
controller.addListener(() async {
await save(controller.text);
});
Corrected
controller.addListener(() async {
try {
await save(controller.text);
} on SaveException catch (error) {
showError(error);
}
});

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 zone of the application reports these failures.
// ignore: assay/async_callback_to_void_parameter

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