prefer_private_named_parameters
Warns when a constructor declares a public named parameter whose only purpose is to initialize a private field of the same name through the initializer list. Since Dart 3.12, a named initializing formal can be private directly (this._name), and callers still use the public name.
Why use this rule
Section titled “Why use this rule”Before Dart 3.12, a named parameter could not start with an underscore, so initializing a private field from a named parameter required boilerplate: declare a public parameter, then assign it in the initializer list. Dart 3.12 removes that restriction — Foo({required this._name}) is valid and is called as Foo(name: ...). The shorter form eliminates a redundant local name, keeps the parameter and field in sync, and cannot drift (e.g. assigning the wrong parameter to the wrong field).
The rule only reports when the conversion is behavior-preserving: the parameter is used solely in that one initializer, its declared type matches the field type, and the library’s language version is 3.12 or later.
See also: Announcing Dart 3.12 | Constructors: Initializing formal parameters
class Bird { final String _petName;
// LINT: petName exists only to initialize _petName Bird({required String petName}) : _petName = petName;}class Bird { final String _petName;
// Callers still write Bird(petName: ...) Bird({required this._petName});}Turning this rule off
Section titled “Turning this rule off”This rule is in the opinionated preset, so it is on with
preset: opinionated, or by name:
rules: prefer_private_named_parameters: trueTo turn it off again:
rules: prefer_private_named_parameters: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Options
Section titled “Options”many_lints: rules: prefer_private_named_parameters: only_same_name: falserules: prefer_private_named_parameters: only_same_name: false| Option | Type | Default | Description |
|---|---|---|---|
only_same_name |
bool | true |
When false, also report a parameter whose name differs from the field (_id from identifier). The quick fix declines those — adopting the shorthand renames the named argument, which breaks call sites |
Related rules
Section titled “Related rules”prefer_named_parameters— Name parameters once there are more than a couple.avoid_accessing_other_classes_private_members— Make the underscore mean what everyone reads it as.avoid_commented_out_code— Detect and flag commented-out code.avoid_complex_conditions— Keep boolean conditions within an operand budget.