type_ literal_ in_ constant_ pattern
Learn about the type_literal_in_constant_pattern linter rule.
Don't use constant patterns with type literals.
Details
#
If you meant to test if the object has type Foo,
instead write Foo _.
BAD:
void f(Object? x) {
if (x case num) {
print('int or double');
}
}
GOOD:
void f(Object? x) {
if (x case num _) {
print('int or double');
}
}
If you do mean to test that the matched value (which you
expect to have the type Type) is equal to the
type literal Foo, then this lint can be silenced
using const (Foo).
BAD:
void f(Object? x) {
if (x case int) {
print('int');
}
}
GOOD:
void f(Object? x) {
if (x case const (int)) {
print('int');
}
}
Enable
#
To enable the
type_literal_in_constant_pattern rule, add
type_literal_in_constant_pattern
under
linter > rules in your
analysis_options.yaml
file:
linter:
rules:
- type_literal_in_constant_pattern
If you're instead using the YAML map syntax to configure
linter rules, add
type_literal_in_constant_pattern: true under
linter > rules:
linter:
rules:
type_literal_in_constant_pattern: true
除非另有说明,文档之所提及适用于 Dart 3.12.2 版本报告页面问题.