Skip to content

prefer_class_destructuring

v0.4.0WarningFixConfigurableCollection & Type

This rule is in the pedantic preset.

When you access three or more properties on the same object within a scope, Dart 3 class destructuring can consolidate those accesses into a single declaration. This makes the code more concise and groups related property extractions together.

Repeated object.property accesses are verbose and scatter related logic across multiple lines. A single destructuring declaration like final MyClass(:name, :email, :age) = object; extracts all needed values at once, making it clear which properties are used in the current scope.

See also: Dart patterns | Destructuring

class UserProfile {
final String name;
final String email;
final int age;
final String address;
const UserProfile({
required this.name,
required this.email,
required this.age,
required this.address,
});
}
// Accessing 3+ properties separately on the same variable
void displayUser(UserProfile user) {
final greeting = 'Hello, ${user.name}';
final contact = user.email;
print('Age: ${user.age}');
}
// Using class destructuring
void displayUser(UserProfile user) {
final UserProfile(:name, :email, :age) = user;
final greeting = 'Hello, $name';
final contact = email;
print('Age: $age');
}
// Only 2 property accesses (below threshold) — no warning
void showBasicInfo(UserProfile user) {
print(user.name);
print(user.email);
}
// Method calls are not counted as property accesses
void interactWithUser(UserProfile user) {
print(user.name);
print(user.email);
user.toString();
}

This rule appears only in the pedantic preset because destructuring can obscure the relationship between a value and its properties.

many_lints.yaml
rules:
prefer_class_destructuring: true

To turn it off again:

many_lints.yaml
rules:
prefer_class_destructuring: false

To keep the rule on but skip certain paths, use per-rule exclude.

analysis_options.yaml
many_lints:
rules:
prefer_class_destructuring:
min_occurrences: 4
ignored_types: [BuildContext, ThemeData]
Option Type Default Description
min_occurrences int 3 Minimum number of distinct property accesses on the same variable before the rule reports
ignored_types list of strings [] Type names never reported, for types where destructuring reads worse than repeated access