prefer_void_callback
v0.4.0 Warning Fix Type Annotations
Flags uses of void Function() that can be replaced with the VoidCallback typedef from dart:ui. The typedef is shorter, more readable, and is the standard Flutter convention for no-argument void callbacks.
Why use this rule
Section titled “Why use this rule”VoidCallback is a well-known typedef in the Flutter framework. Using it instead of the verbose 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: VoidCallback typedef
class BadWidget { final void Function() onTap; final void Function()? onLongPress;
const BadWidget(this.onTap, this.onLongPress);}
void badParameter(void Function() callback) {}
void Function() badReturnType() => () {};
List<void Function()> callbacks = [];class GoodWidget { final VoidCallback onTap; final VoidCallback? onLongPress;
const GoodWidget(this.onTap, this.onLongPress);}
// Function types with parameters or different return types are fine:void goodWithParams(void Function(int value) callback) {}int Function() goodIntReturn = () => 0;Future<void> Function() goodFutureReturn = () async {};Configuration
Section titled “Configuration”This rule is in the opinionated preset, so it is on with
preset: opinionated, or by name:
rules: prefer_void_callback: trueTo turn it off again:
rules: prefer_void_callback: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”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.prefer_typedefs_for_callbacks— Name a multi-parameter function type with a typedef.prefer_async_callback— Use ‘AsyncCallback’ instead of ‘Future<void> Function()’.