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) {}
}
Unless stated otherwise, the documentation on this site reflects Dart 3.7.3. Page last updated on 2025-05-08. View source or report an issue.