type_literal_in_constant_pattern
Use 'TypeName _' instead of a type literal.
Description
#The analyzer produces this diagnostic when a type literal appears as a pattern.
Example
#The following code produces this diagnostic because a type literal is used as a constant pattern:
void f(Object? x) {
if (x case num) {
// ...
}
}
Common fixes
#If the type literal is intended to match an object of the given type, then use either a variable pattern:
void f(Object? x) {
if (x case num _) {
// ...
}
}
Or an object pattern:
void f(Object? x) {
if (x case num()) {
// ...
}
}
If the type literal is intended to match the type literal, then write it as a constant pattern:
void f(Object? x) {
if (x case const (num)) {
// ...
}
}
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.