Skip to content

prefer_wildcard_pattern

v0.4.0 Warning Fix Pattern Matching

Using Object() as a catch-all pattern in switch expressions, switch statements, or if-case conditions is functionally equivalent to the wildcard pattern _. The wildcard is more idiomatic in Dart and instantly recognizable as “match anything.”

_ is the standard Dart idiom for “I don’t care about the value.” Using Object() instead adds visual noise and may confuse readers into thinking the pattern is doing something specific. The wildcard pattern is shorter, clearer, and universally understood.

See also: Dart patterns

// Using Object() as a catch-all pattern
String classify(Object object) {
return switch (object) {
int() => 'int',
Object() => 'other',
};
}
void statement(Object object) {
switch (object) {
case int():
break;
case Object():
break;
}
}
void ifCase(Object object) {
if (object case Object()) {}
}
// Using the wildcard pattern _
String classify(Object object) {
return switch (object) {
int() => 'int',
_ => 'other',
};
}
void statement(Object object) {
switch (object) {
case int():
break;
case _:
break;
}
}
// Object() with field destructuring is fine — it extracts values
String withFields(Object object) {
return switch (object) {
int() => 'int',
Object(hashCode: final h) => 'hash: $h',
_ => 'other',
};
}

This rule is in the recommended preset, so it is on with preset: recommended or preset: opinionated. Add it to preset: core with prefer_wildcard_pattern: true.

To turn it off:

many_lints.yaml
rules:
prefer_wildcard_pattern: false

To keep the rule on but skip certain paths, use per-rule exclude.