non_constant_map_pattern_key
Key expressions in map patterns must be constants.
Description
#The analyzer produces this diagnostic when a key in a map pattern isn't a constant expression.
Example
#The following code produces this diagnostic because the key A()
isn't a constant:
dart
void f(Object x) {
if (x case {A(): 0}) {}
}
class A {
const A();
}
Common fixes
#Use a constant for the key:
dart
void f(Object x) {
if (x case {const A(): 0}) {}
}
class A {
const A();
}
Was this page's content helpful?
Thank you for your feedback!
Provide details Thank you for your feedback! Please let us know what we can do to improve.
Provide details Unless stated otherwise, the documentation on this site reflects Dart 3.8.1. Page last updated on 2025-05-08. View source or report an issue.