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.
Why use this rule
Section titled “Why use this rule”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 = () {};Configuration
Section titled “Configuration”This rule is in the opinionated preset, so it is on with
preset: opinionated, or by name:
rules: prefer_async_callback: trueTo turn it off again:
rules: prefer_async_callback: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”prefer_void_callback— Use ‘VoidCallback’ instead of ‘void Function()’.prefer_equatable_mixin— Prefer using EquatableMixin instead of extending Equatable.prefer_explicit_function_type— Prefer explicit function type annotations over the bare ‘Function’ type.prefer_explicit_parameter_names— Name the parameters of a function type.