1

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?

enter image description here

enter image description here

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();
 }
}

enter image description here

asked Jul 16, 2022 at 6:35
5
  • 1
    Are you sure your reproduction is accurate? one and another have equal hashCode values in DartPad. Commented Jul 16, 2022 at 7:35
  • 2
    It's not that this const List has non-const elements; 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 to ConstItem deriving from Widget. Commented Jul 16, 2022 at 7:44
  • 1
    I would recommend filing an issue. Commented Jul 16, 2022 at 7:57
  • @hacker1024 It is correct sample. I have edited the post and provided image with proof Commented Jul 16, 2022 at 9:12
  • Closing the loop: OP filed github.com/flutter/flutter/issues/107767, where it was explained that it's normal for debug builds: docs.flutter.dev/testing/… Commented Jul 19, 2022 at 17:43

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.