Skip to content

prefer_typedefs_for_callbacks

v1.0.0WarningConfigurableType Annotations

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) {}

This rule is in the pedantic preset, so it is enabled by preset: pedantic or by name:

many_lints.yaml
rules:
prefer_typedefs_for_callbacks:
enabled: true
analysis_options.yaml
many_lints:
rules:
prefer_typedefs_for_callbacks:
min_parameters: 2
Option Type Default Description
min_parameters int 2 How many parameters an inline function type may have

To disable this rule:

many_lints.yaml
rules:
prefer_typedefs_for_callbacks: false

To keep the rule on but skip certain paths, use per-rule exclude.