Skip to main content

match-positional-field-names-on-assignment

added in: 1.5.0
🛠
Dart 3.0+
Pro+

Warns when a positional field name does not match a variable name on destructuring assignment.

Records syntax allows to declare two type of fields: named and positional (which can also have a name). This rule reuses the name of the positional field and requires it to be matched on any destructuring assignment.

This rule can help you reduce the verbosity of named fields, but also ensure the names are consistent with the record type declaration.

warning

This rule is not compatible with avoid-redundant-positional-field-name.

Example

❌ Bad:

(int x, int y) getPoint() => (1, 1);

void main() {
final (y, x) = getPoint(); // LINT: Prefer matching record positional field name. Try renaming it.

final (first, second) = getPoint(); // LINT: Prefer matching record positional field name. Try renaming it.
}

✅ Good:

(int,) calculate() => (0,);

(int x, int y) getPoint() => (1, 1);

({int x, int y}) getAnotherPoint() => (x: 1, y: 1);

void main() {
final (value, ) = calculate(); // Correct, positional field has no name

final (x, y) = getPoint(); // Correct, names of positional fields are matched

final (x: first, y: second) = getAnotherPoint(); // Correct, named fields
}