I'm creating a test with const Widget list.
const passing = [
Item(),
Item(),
];
But test fails. I found out that items are non const! Their hashCodes are different as if they are (new). Why?
Reproducing
import 'package:flutter/material.dart';
import 'package:test/test.dart';
void main() {
test('Non equal consts', () {
final one = constList[0];
final another = constList[1];
expect(one, another);
});
}
const constList = [
ConstItem(),
ConstItem(),
];
class ConstItem extends Widget {
const ConstItem({Key? key}) : super(key: key);
@override
Element createElement() {
throw UnimplementedError();
}
}
lang-dart
oneandanotherhave equalhashCodevalues in DartPad.constListhas non-constelements; the elements surely are immutable. What you're observing is that the elements are not canonicalized. I can reproduce this when I run it locally, and it seems to be tied toConstItemderiving fromWidget.