Skip to main content

match-positional-field-names-on-assignment

added in: 1.5.0
🛠
Dart 3.0+

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,) calculate() => (0,);

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

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

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

final (y, x) = getPoint(); // LINT

final (first, second) = getPoint(); // LINT

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

✅ 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(); // <-- good, positional field has no name

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

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