Skip to content

prefer_async_callback

v0.4.0 Warning Fix Type Annotations

Flags Future<void> Function() written where the AsyncCallback typedef from package:flutter/foundation.dart would say the same thing. The quick fix replaces the type and adds the import.

Only the exact shape is reported: no parameters, and a Future<void> return. Future<int> Function() and Future<void> Function(int) are left alone — there is no typedef for them.

A callback field on a widget, spelled out longhand:

class UploadButton {
const UploadButton({required this.onPressed, this.onCancel});
final Future<void> Function() onPressed;
final Future<void> Function()? onCancel;
}

The same type in a function signature and a return type:

void retryOnFailure(Future<void> Function() action) {}
Future<void> Function() saveDraftAction(String draftId) =>
() async {};

And in a collection of pending work:

final List<Future<void> Function()> pendingUploads = [];
import 'package:flutter/foundation.dart';
class UploadButton {
const UploadButton({required this.onPressed, this.onCancel});
final AsyncCallback onPressed;
final AsyncCallback? onCancel;
}
void retryOnFailure(AsyncCallback action) {}
AsyncCallback saveDraftAction(String draftId) => () async {};
final List<AsyncCallback> pendingUploads = [];

AsyncCallback is exactly Future<void> Function(), so anything with a parameter or a different return type has no typedef to swap in:

// A result to await, not just completion.
Future<int> Function() fetchCount = () async => 0;
// Takes an argument.
Future<void> Function(int index) removeAt = (_) async {};
// Synchronous — that one is `VoidCallback`; see prefer_void_callback.
void Function() dismiss = () {};

See also: AsyncCallback typedef

This rule is in the opinionated preset, so it is on with preset: opinionated, or by name:

many_lints.yaml
rules:
prefer_async_callback: true

To turn it off again:

many_lints.yaml
rules:
prefer_async_callback: false

To keep the rule on but skip certain paths, use per-rule exclude.