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”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:
rules: prefer_wildcard_pattern: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”avoid_wildcard_cases_with_enums— Opposing convention. Keep exhaustiveness checking by listing enum cases explicitly.prefer_switch_with_enums— Use a switch instead of an if-else chain over enum constants.avoid_single_field_destructuring— Avoid destructuring a single field when direct property access is simpler.use_existing_destructuring— Add properties to an existing destructuring instead of accessing them directly.