Skip to content

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.

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 types
final void Function(int value) onValueChanged = (_) {};
final int Function(String input) processInput = (_) => 0;

To disable this rule:

plugins:
many_lints:
diagnostics:
prefer_explicit_function_type: false