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.”
Why use this rule
Section titled “Why use this rule”_ 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 patternString 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 valuesString withFields(Object object) { return switch (object) { int() => 'int', Object(hashCode: final h) => 'hash: $h', _ => 'other', };}Configuration
Section titled “Configuration”To disable this rule:
plugins: many_lints: diagnostics: prefer_wildcard_pattern: false