prefer_shorthands_with_static_fields
v0.3.0 Warning Fix Shorthand Patterns
Flags explicit class prefixes on static field accesses (e.g., SomeClass.first) when the type can be inferred from context and a dot shorthand (.first) 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 SomeClass { final String value; const SomeClass(this.value); static const first = SomeClass('first'); static const second = SomeClass('second');}
void example(SomeClass? e) { switch (e) { case SomeClass.first: print(e); }
final SomeClass another = SomeClass.first; if (e == SomeClass.first) {}}
void fn({SomeClass value = SomeClass.first}) {}
SomeClass getResult() => SomeClass.first;void example(SomeClass? e) { switch (e) { case .first: print(e); }
final SomeClass another = .first; if (e == .first) {}}
void fn({SomeClass value = .first}) {}
SomeClass getResult() => .first;
// Explicit prefix is fine when type cannot be inferred:Object getObject() => SomeClass.first;Configuration
Section titled “Configuration”To disable this rule:
plugins: many_lints: diagnostics: prefer_shorthands_with_static_fields: false