Skip to main content

prefer-wildcard-pattern

added in: 1.5.0
🛠
Dart 3.0+
Pro+
preset: recommended

Warns when a dynamic or Object type is used instead of a wildcard pattern.

Although it's not a mistake to use them, patterns provide a better option to match unknown value called a wildcard pattern _.

Example

❌ Bad:

final object = WithField('hello');

final value = switch (object) {
WithField() => 'good',
Object() => 'bad', // LINT: Prefer the wildcard pattern '_' instead.
dynamic() => 'bad', // LINT: Prefer the wildcard pattern '_' instead.
};

✅ Good:

final object = WithField('hello');

final value = switch (object) {
WithField() => 'good',
_ => 'good',
};