Skip to main content

avoid-positional-record-field-access

added in: 1.5.0
Dart 3.0+
Pro+

Warns when a record positional field is accessed via $.

Accessing record positional fields via $1, $2, etc. might look like a great shortcut to write less code, but it also brings readability issues for others who read the code.

Instead, consider destructuring (final (x, y) = ...) the record or converting its fields to named fields.

Example

❌ Bad:

void function() {
final record = (
'hello',
'world',
);

final first = record.$1; // LINT: Avoid accessing record fields via $. Try using an intermediate variable or converting record fields to named fields.
final second = record.$2; // LINT: Avoid accessing record fields via $. Try using an intermediate variable or converting record fields to named fields.
}

✅ Good:

void function() {
final record = (
first: 'hello',
second: 'world',
);

final first = record.first; // Correct, named field
final second = record.second;

final another = (
'hello',
'world',
);

final (first, second) = another;
}