Skip to main content

prefer-shorthands-with-static-fields

effort: 2m
dart 3.10+
has IDE fix
has auto-fix
starter+

Suggests using dot shorthands with static fields.

When a static field type matches the type of its class, such fields can be used as dot shorthands (without an explicit class prefix).

Example

❌ Bad:

void fn(SomeClass? e) {
switch (e) {
// LINT: Prefer dot shorthands instead of explicit class prefixes. Try removing the prefix.
case SomeClass.first:
print(e);
}

final v = switch (e) {
// LINT: Prefer dot shorthands instead of explicit class prefixes. Try removing the prefix.
SomeClass.first => 1,
_ => 2,
};

// LINT: Prefer dot shorthands instead of explicit class prefixes. Try removing the prefix.
final SomeClass another = SomeClass.first;

// LINT: Prefer dot shorthands instead of explicit class prefixes. Try removing the prefix.
if (e == SomeClass.first) {}
}

// LINT: Prefer dot shorthands instead of explicit class prefixes. Try removing the prefix.
void another({SomeClass value = SomeClass.first}) {}

// LINT: Prefer dot shorthands instead of explicit class prefixes. Try removing the prefix.
SomeClass getClass() => SomeClass.first;

class SomeClass {
final String value;

const SomeClass(this.value);

static const first = SomeClass('first');
static const second = SomeClass('second');
}

✅ Good:

void fn(SomeClass? e) {
switch (e) {
case .first:
print(e);
}

final v = switch (e) {
.first => 1,
_ => 2,
};

final SomeClass another = .first;

if (e == .first) {}
}

void another({SomeClass value = .first}) {}

SomeClass getClass() => .first;

Object getObject() => SomeClass.first;