Skip to content

prefer_private_named_parameters

v0.7.0WarningFixConfigurableCode Quality

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.

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

This rule is in the opinionated preset, so it is on with preset: opinionated, or by name:

many_lints.yaml
rules:
prefer_private_named_parameters: true

To turn it off again:

many_lints.yaml
rules:
prefer_private_named_parameters: false

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

analysis_options.yaml
many_lints:
rules:
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