type_literal_in_constant_pattern
Don't use constant patterns with type literals.
此规则自 Dart 3.0 版本起可用。
_规则集:core , recommended , flutter _
此规则提供 快速修复 。
详情
#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');
}
}
使用方法
#要启用 type_literal_in_constant_pattern
规则,请在你的 analysis_options.yaml
文件中,在 linter > rules 下添加 type_literal_in_constant_pattern
:
analysis_options.yaml
yaml
linter:
rules:
- type_literal_in_constant_pattern
除非另有说明,否则本网站上的文档反映的是 Dart 3.6.0。页面最后更新于 2025-02-05。 查看源代码 或 报告问题.