prefer_typedefs_for_callbacks
This rule flags an inline function type written where a typedef would name it.
void Function(String, int, bool) tells the reader the shape and nothing else — what the String is, what the bool means, and whether two such parameters are the same concept. A typedef gives the signature a name that can be reused, documented and searched for.
Only function types with at least min_parameters parameters are reported. void Function() and void Function(String) are already readable, and naming them adds indirection without adding information — which is why Flutter’s own VoidCallback and ValueChanged<T> exist for exactly the shapes that clear this bar.
This rule is in the pedantic preset.
void listen(void Function(String, int) onEvent) {}typedef EventHandler = void Function(String name, int code);
void listen(EventHandler onEvent) {}Enabling this rule
Section titled “Enabling this rule”This rule is in the pedantic preset, so it is enabled by preset: pedantic or by name:
rules: prefer_typedefs_for_callbacks: enabled: trueOptions
Section titled “Options”many_lints: rules: prefer_typedefs_for_callbacks: min_parameters: 2rules: prefer_typedefs_for_callbacks: min_parameters: 2| Option | Type | Default | Description |
|---|---|---|---|
min_parameters |
int | 2 |
How many parameters an inline function type may have |
Turning this rule off
Section titled “Turning this rule off”To disable this rule:
rules: prefer_typedefs_for_callbacks: 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_void_callback— Use ‘VoidCallback’ instead of ‘void Function()’.prefer_async_callback— Use ‘AsyncCallback’ instead of ‘Future<void> Function()’.