constant_pattern_never_matches_value_type
The matched value type '{0}' can never be equal to this constant of type '{1}'.
Description
#The analyzer produces this diagnostic when a constant pattern can never match the value it's being tested against because the type of the constant is known to never match the type of the value.
Example
#The following code produces this diagnostic because the type of the
constant pattern (true)
is bool
, and the type of the value being
matched (x
) is int
, and a Boolean can never match an integer:
void f(int x) {
if (x case true) {}
}
Common fixes
#If the type of the value is correct, then rewrite the pattern to be compatible:
void f(int x) {
if (x case 3) {}
}
If the type of the constant is correct, then rewrite the value to be compatible:
void f(bool x) {
if (x case true) {}
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。