type_literal_in_constant_pattern
Don't use constant patterns with type literals.
This rule is available as of Dart 3.0.
Rule sets: core, recommended, flutter
This rule has a quick fix available.
Details
#If you meant to test if the object has type Foo
, instead write Foo _
.
BAD:
dart
void f(Object? x) {
if (x case num) {
print('int or double');
}
}
GOOD:
dart
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:
dart
void f(Object? x) {
if (x case int) {
print('int');
}
}
GOOD:
dart
void f(Object? x) {
if (x case const (int)) {
print('int');
}
}
Usage
#To enable the type_literal_in_constant_pattern
rule,
add type_literal_in_constant_pattern
under linter > rules in your
analysis_options.yaml
file:
analysis_options.yaml
yaml
linter:
rules:
- type_literal_in_constant_pattern
除非另有说明,文档之所提及适用于 Dart 3.5.4 版本,本页面最后更新时间: 2024-08-02。 查看文档源码 或者 报告页面问题。