prefer_explicit_parameter_names
This rule flags a function type — in a typedef or a function-typed parameter — that declares unnamed parameters.
void Function(String, int) tells a reader the types and nothing else. At the point where someone writes the callback, (String, int) gives them two anonymous values to guess at — is the int an index, a count, an id? void Function(String label, int count) answers that in the signature, where the answer belongs, and IDEs echo those names into the closure they generate.
This rule is in the pedantic preset, and pedantic supplies min_parameters: 1 — so under that preset a single-parameter function type is reported too. The rule’s own default when you enable it by name is 2.
A callback whose parameters are anonymous, in a typedef and at a call boundary:
typedef OnChanged = void Function(String, int);
void listen(void Function(String, int) callback) {}The reader writing listen((a, b) { ... }) has nothing to go on.
typedef OnChanged = void Function(String label, int count);
void listen(void Function(String label, int count) callback) {}Naming one and not the other is still reported — the rule fires when any parameter is unnamed:
// Don't — `int` is still anonymoustypedef OnChanged = void Function(String label, int);
// Dotypedef OnChanged = void Function(String label, int count);Single-parameter types under preset: pedantic
Section titled “Single-parameter types under preset: pedantic”pedantic sets min_parameters: 1, so this is reported:
rules: prefer_explicit_parameter_names: min_parameters: 1// Don'ttypedef OnTap = void Function(String);
// Dotypedef OnTap = void Function(String routeName);Exempting single-parameter types
Section titled “Exempting single-parameter types”Set min_parameters: 2 to get the rule’s own default back — void Function(T)
is unambiguous from the type alone:
rules: prefer_explicit_parameter_names: min_parameters: 2// Not reported at `min_parameters: 2`typedef OnTap = void Function(String);See also: Effective Dart: type annotations
Options
Section titled “Options”many_lints: rules: prefer_explicit_parameter_names: min_parameters: 2rules: prefer_explicit_parameter_names: min_parameters: 2| Option | Type | Default | Description |
|---|---|---|---|
min_parameters |
int | 2 |
Fewest parameters a function type must have before names are required. preset: pedantic overrides this to 1 |
Turning this rule off
Section titled “Turning this rule off”To disable this rule:
rules: prefer_explicit_parameter_names: 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_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.