prefer_class_destructuring
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.
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();}Turning this rule off
Section titled “Turning this rule off”This rule appears only in the pedantic preset because destructuring can
obscure the relationship between a value and its properties.
rules: prefer_class_destructuring: trueTo turn it off again:
rules: prefer_class_destructuring: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Options
Section titled “Options”many_lints: rules: prefer_class_destructuring: min_occurrences: 4 ignored_types: [BuildContext, ThemeData]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 |
Related rules
Section titled “Related rules”avoid_accessing_collections_by_constant_index— Avoid accessing a collection by a constant index inside a loop.avoid_collection_equality_checks— Avoid comparing collections with == or != as it checks reference equality, not contents.avoid_collection_methods_with_unrelated_types— Avoid calling collection methods with arguments whose types are unrelated to the collection’s type parameter.avoid_duplicate_collection_elements— Don’t repeat the same element in a collection literal.