prefer_shorthands_with_static_fields
v0.3.0 Warning Fix Shorthand Patterns
Flags explicit class prefixes on static field accesses (e.g., Currency.usd) when the type can be inferred from context and a dot shorthand (.usd) would suffice. This applies to switch cases, switch expressions, typed variable declarations, comparisons, default parameters, and return expressions.
Why use this rule
Section titled “Why use this rule”When the expected type is already known from context, repeating the class name on a static field access adds visual noise. Dot shorthands are more concise and keep the focus on the value rather than the type. This rule skips enums, which are handled separately by prefer_shorthands_with_enums.
class Currency { final String code; const Currency(this.code); static const usd = Currency('USD'); static const eur = Currency('EUR');}
void example(Currency? e) { switch (e) { case Currency.usd: print(e); }
final Currency another = Currency.usd; if (e == Currency.usd) {}}
void fn({Currency value = Currency.usd}) {}
Currency getResult() => Currency.usd;void example(Currency? e) { switch (e) { case .usd: print(e); }
final Currency another = .usd; if (e == .usd) {}}
void fn({Currency value = .first}) {}
Currency getResult() => .usd;
// Explicit prefix is fine when type cannot be inferred:Object getObject() => Currency.usd;Configuration
Section titled “Configuration”This rule is in the opinionated preset, so it is on with
preset: opinionated, or by name:
rules: prefer_shorthands_with_static_fields: trueTo turn it off again:
rules: prefer_shorthands_with_static_fields: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”avoid_nested_shorthands— Avoid nesting a dot shorthand inside another dot shorthand invocation.prefer_returning_shorthands— Use dot shorthand constructors in expression function return values.prefer_shorthands_with_constructors— Use dot shorthand constructors for common Flutter classes.prefer_shorthands_with_enums— Use dot shorthands instead of explicit enum prefixes.