I've got ClassA which holds a a List<ClassB>. ClassB has an string attribute.
If I now have one const Object of ClassA with a list of an object of ClassB completly identical to another non const Object of ClassA with the exact same object of ClassB then these two are not treated as equal.
Why? I could not find any documentation referencing this occurance when looking any documentation regarding equality.
Here's the code:
import 'package:test/test.dart';
void main() {
test('equal', () {
const ClassA a1 = ClassA(list: [ClassB(text: "Mo")]);
ClassA a2 = ClassA(list: [ClassB(text: "Mo"),]);
expect(const [ClassB(text: "Mo")], [ClassB(text: "Mo")]);//true
expect(a1, equals(a2)); //false. Is only true when a2 is const.
});
}
class ClassB {
final String text;
const ClassB({this.text});
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is ClassB &&
runtimeType == other.runtimeType &&
text == other.text;
@override
int get hashCode => text.hashCode;
}
class ClassA {
final List<ClassB> list;
const ClassA({this.list});
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is ClassA &&
runtimeType == other.runtimeType &&
list == other.list;
@override
int get hashCode => list.hashCode;
}
I expected a1 and a2 as being equal.