Skip to main content

avoid-mixing-named-and-positional-fields

added in: 1.5.0
Dart 3.0+
Pro+

Warns when a record declaration contains both named and positional fields.

Mixing named and positional fields can lead to confusion when working with such records (as some fields will have a name, but other will be just $1, $2, etc.), especially if some of the fields have the same type.

Example

❌ Bad:

final record = ('hello',);
final record = ('hello', hi: 'world'); // LINT: Avoid records with both named and positional fields. Try converting all fields to named.

class MyClass {
final (String, {int named}) field; // LINT: Avoid records with both named and positional fields. Try converting all fields to named.

const MyClass(this.field);

(int, {int named}) calculate() => (1, named: 0); // LINT: Avoid records with both named and positional fields. Try converting all fields to named.
}

✅ Good:

final record = ('hello',);
final record = (first: 'hello', second: 'world'); // Correct, both fields are named

class MyClass {
final ({String value, int named}) field;

const MyClass(this.field);

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