Skip to main content

avoid-mixing-named-and-positional-fields

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',);
// LINT: Avoid records with both named and positional fields. Try converting all fields to named.
final record = ('hello', hi: 'world');

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

const MyClass(this.field);

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

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