avoid_single_field_destructuring
v0.4.0 Warning Fix Pattern Matching
Destructuring a single field from an object or record pattern adds unnecessary syntactic overhead. Direct property access like config.name is simpler and clearer when only one value is needed.
Why use this rule
Section titled “Why use this rule”Destructuring shines when extracting multiple values at once. For a single field, final Config(:name) = config; is more verbose than final name = config.name; with no readability benefit. This rule encourages destructuring only when it genuinely simplifies the code.
See also: Dart patterns
class Config { final String name; final int timeout;
const Config({required this.name, required this.timeout});}
void example(Config config) { // Single field destructured from object pattern final Config(:name) = config;
// Single named field destructured with renamed variable final Config(timeout: t) = config;}
void recordExample(({int length}) record) { // Single field destructured from record pattern final (:length) = record;}// Direct property accessvoid example(Config config) { final name = config.name; final t = config.timeout;}
// Multiple fields destructured (this is the right use case)void multipleFields(Config config) { final Config(:name, :timeout) = config;}
// Regular variable declaration (no destructuring)void regularDeclaration() { final x = 42;}Configuration
Section titled “Configuration”To disable this rule:
plugins: many_lints: diagnostics: avoid_single_field_destructuring: false