prefer_explicit_function_type
v0.3.0 Warning Fix Type Annotations
Flags uses of the bare Function type that do not specify a return type or parameter list. Using the unparameterized Function type effectively makes the declaration dynamic and disables type checking on calls, which can hide bugs.
Why use this rule
Section titled “Why use this rule”The bare Function type accepts any number and type of arguments and returns dynamic, bypassing Dart’s type system entirely. Specifying the return type and parameter list catches mismatched signatures at compile time rather than at runtime.
See also: Dart language - Function type
class BadWidget { final Function onTap; final Function? onLongPress;
const BadWidget(this.onTap, this.onLongPress);}
void badFunction(Function callback) {}
Function badReturnType() => () {};
List<Function> callbacks = [];class GoodWidget { final void Function() onTap; final void Function()? onLongPress;
const GoodWidget(this.onTap, this.onLongPress);}
void goodFunction(void Function() callback) {}
void Function() goodReturnType() => () {};
List<void Function()> callbacks = [];
// Function types with parameters and return typesfinal void Function(int value) onValueChanged = (_) {};final int Function(String input) processInput = (_) => 0;Configuration
Section titled “Configuration”This rule is in the recommended preset, so it is on with
preset: recommended or preset: opinionated. Add it to preset: core with
prefer_explicit_function_type: true.
To turn it off:
rules: prefer_explicit_function_type: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”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_void_callback— Use ‘VoidCallback’ instead of ‘void Function()’.prefer_explicit_type_arguments— Pin the type arguments of the APIs where inference surprises.