Skip to main content

prefer-class-destructuring

dart 3.0+
configurable
pro+

Warns when several different property accesses can be replaced by a single class destructuring declaration.

Using class destructuring can help simplify the code and reduce duplicate property accesses.

⚙️ Config

Set min-occurrences (default is 3) to configure the minimum number of occurrences after which the class destructuring should be suggested.

Set ignored-types (default is empty) to exclude certain types of the variable .

analysis_options.yaml
dart_code_metrics:
rules:
- prefer-class-destructuring:
min-occurrences: 3
ignored-types:
- SomeClass

Example

❌ Bad:

void fn(SomeClass variable) {
// LINT: Prefer using class destructuring instead of accessing several different properties.
final value = variable.another;
// LINT: Prefer using class destructuring instead of accessing several different properties.
final another = variable.value;

print(variable.inner);// LINT: Prefer using class destructuring instead of accessing several different properties.
}

✅ Good:

void fn(SomeClass variable) {
final SomeClass(:another, :value, :inner) = variable;
}