const_ set_ element_ not_ primitive_ equality
(Previously known as const_set_element_type_implements_equals
)
An element in a constant set can't override the '==' operator, or 'hashCode', but the type '{0}' does.
Description
#
The analyzer produces this diagnostic when the class of object used as an
element in a constant set literal implements either the
==
operator, the
getter
hashCode
, or both. The implementation of constant sets 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.
Example
#
The following code produces this diagnostic because the constant set
contains an element whose type is
C
, and the class
C
overrides the
implementation of
==
:
class C {
const C();
bool operator ==(Object other) => true;
}
const set = {C()};
The following code produces this diagnostic because the constant set
contains an element whose type is
C
, and the class
C
overrides the
implementation of
hashCode
:
class C {
const C();
int get hashCode => 3;
}
const map = {C()};
Common fixes
#
If you can remove the implementation of
==
and
hashCode
from the
class, then do so:
class C {
const C();
}
const set = {C()};
If you can't remove the implementation of
==
and
hashCode
from the
class, then make the set non-constant:
class C {
const C();
bool operator ==(Object other) => true;
}
final set = {C()};
Unless stated otherwise, the documentation on this site reflects Dart 3.9.2. Page last updated on 2025-9-4. View source or report an issue.