Skip to content

prefer_async_callback

v0.4.0 Warning Fix Type Annotations

Flags uses of Future<void> Function() that can be replaced with the AsyncCallback typedef from package:flutter/foundation.dart. The typedef is shorter, more readable, and is the standard Flutter convention for no-argument async void callbacks.

AsyncCallback is a well-known typedef in the Flutter framework. Using it instead of the verbose Future<void> Function() makes code more concise and consistent with the rest of the Flutter ecosystem. The quick fix automatically replaces the type and adds the necessary import.

See also: AsyncCallback typedef

class BadWidget {
final Future<void> Function() onTap;
final Future<void> Function()? onLongPress;
const BadWidget(this.onTap, this.onLongPress);
}
void badParameter(Future<void> Function() callback) {}
Future<void> Function() badReturnType() => () async {};
List<Future<void> Function()> callbacks = [];
class GoodWidget {
final AsyncCallback onTap;
final AsyncCallback? onLongPress;
const GoodWidget(this.onTap, this.onLongPress);
}
// Function types with different return types or parameters are fine:
Future<int> Function() goodFutureInt = () async => 0;
Future<void> Function(int value) goodWithParams = (_) async {};
void Function() goodVoidCallback = () {};

To disable this rule:

plugins:
many_lints:
diagnostics:
prefer_async_callback: false