Skip to content

prefer_class_destructuring

v0.4.0 Warning Fix Collection & Type

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

To disable this rule:

plugins:
many_lints:
diagnostics:
prefer_class_destructuring: false