const_map_key_not_primitive_equality
The type of a key in a constant map can't override the '==' operator, or 'hashCode', but the class '{0}' does.
Description
#The analyzer produces this diagnostic when the class of object used as a
key in a constant map literal implements either the ==
operator, the
getter hashCode
, or both. The implementation of constant maps uses both
the ==
operator and the hashCode
getter, so any implementation other
than the ones inherited from Object
requires executing arbitrary code at
compile time, which isn't supported.
Examples
#The following code produces this diagnostic because the constant map
contains a key whose type is C
, and the class C
overrides the
implementation of ==
:
class C {
const C();
bool operator ==(Object other) => true;
}
const map = {C() : 0};
The following code produces this diagnostic because the constant map
contains a key whose type is C
, and the class C
overrides the
implementation of hashCode
:
class C {
const C();
int get hashCode => 3;
}
const map = {C() : 0};
Common fixes
#If you can remove the implementation of ==
and hashCode
from the
class, then do so:
class C {
const C();
}
const map = {C() : 0};
If you can't remove the implementation of ==
and hashCode
from the
class, then make the map non-constant:
class C {
const C();
bool operator ==(Object other) => true;
}
final map = {C() : 0};
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。