Skip to main content

avoid-single-field-destructuring

added in: 1.25.0
Dart 3.0+
Pro+

Suggests using a regular variable declaration instead of a single field destructuring.

It's recommended to use the good old variable declaration when it comes to a single field destructuring, as it can be confusing when it comes to order of execution (e.g. a destructured prop is executed after the expression, but is placed on the left-hand side) and usually requires importing the class declaration.

Example

❌ Bad:

void fn(SomeClass input) {
// LINT: Avoid pattern destructuring with only one field. Try accessing this field directly.
final SomeClass(:value) = input;

// LINT: Avoid pattern destructuring with only one field. Try accessing this field directly.
final List(:length) = [input];
}

class SomeClass {
final String value;
...
}

✅ Good:

void fn(SomeClass input) {
final length = [input].length;

final SomeClass(:value, :inner) = input;
}