prefer_typedefs_for_callbacks
This rule flags an inline function type with two or more parameters, 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.
This rule is in the pedantic preset. min_parameters is the threshold at which reporting starts: at the default 2, a two-parameter inline type is already reported, while void Function() and void Function(String) are not. That is why Flutter ships VoidCallback and ValueChanged<T> for those shapes and stops there.
An event callback carried through a signature, spelled out inline at every hop:
void listen(void Function(String, int) onEvent) {}
class EventBus { EventBus(this.onEvent);
final void Function(String, int) onEvent;}Name it once, then use the name:
typedef EventHandler = void Function(String name, int code);
void listen(EventHandler onEvent) {}
class EventBus { EventBus(this.onEvent);
final EventHandler onEvent;}One parameter or none is never reported
Section titled “One parameter or none is never reported”At the default min_parameters: 2, these are below the threshold and pass as
written:
void onTap(void Function() callback) {}
void onChanged(void Function(String value) callback) {}The typedef’s own body is exempt
Section titled “The typedef’s own body is exempt”Declaring the typedef is the fix, so the function type inside it is never reported however many parameters it has:
typedef ProgressReport = void Function(int sent, int total, bool done);Reporting single-parameter types too
Section titled “Reporting single-parameter types too”With min_parameters: 1, a one-parameter inline type is reported as well:
rules: prefer_typedefs_for_callbacks: min_parameters: 1// Don'tvoid onChanged(void Function(String value) callback) {}
// Dotypedef TextChanged = void Function(String value);
void onChanged(TextChanged callback) {}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 |
Fewest parameters an inline function type must have before it is reported. At 2, a two-parameter type is reported |
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()’.