@override
Widget build(BuildContext context) {
const one = SizedBox();
const two = SizedBox();
print('${identical(one, two)}'); // prints false, should print true
return Container();
}
Am I doing something wrong?
@override
Widget build(BuildContext context) {
const one = SizedBox();
const two = SizedBox();
print('${identical(one, two)}'); // prints false, should print true
return Container();
}
Am I doing something wrong?
Nope, you're not doing something wrong. The answer comes out false simply because they are two entirely separate objects when stored in memory.
identical and similar methods used to check for equality only see if the underlying objects or reference variables are the same and not the classes they belong to.
Proof:
Widget build(BuildContext context) {
const one = const SizedBox();
const two = const SizedBox();
print(one.hashCode);
print(two.hashCode);
print('${identical(one, two)}'); //
print('${one==two?'t':'f'}');
return MaterialApp(home: Container(child: Text('_'),));
Output:
I/flutter ( 3657): 779669181
I/flutter ( 3657): 731265968
I/flutter ( 3657): false
I/flutter ( 3657): f