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.
Why use this rule
Section titled “Why use this rule”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 variablevoid displayUser(UserProfile user) { final greeting = 'Hello, ${user.name}'; final contact = user.email; print('Age: ${user.age}');}// Using class destructuringvoid 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 warningvoid showBasicInfo(UserProfile user) { print(user.name); print(user.email);}
// Method calls are not counted as property accessesvoid interactWithUser(UserProfile user) { print(user.name); print(user.email); user.toString();}Configuration
Section titled “Configuration”To disable this rule:
plugins: many_lints: diagnostics: prefer_class_destructuring: false