Lets say in Flutter we have a Custom Painter like this that takes a List as an argument:
class StackPainter02 extends CustomPainter {
final List<double> brightnessValues;
late final int numberOfBulbs;
StackPainter02(
{required this.brightnessValues}) {
numberOfBulbs = brightnessValues.length;
}
@override
void paint(Canvas canvas, Size size) {
// ...
}
@override
bool shouldRepaint(covariant StackPainter02 oldDelegate) {
return (oldDelegate.brightnessValues != brightnessValues); // ??? does not seem to work
//return true;
}
}
In the shouldRepaint(), I suspect the reason this isn't working has to do with how comparing lists works in Dart and that comparing two lists will always return false even if their contents are the same.
... and that we have to use some type of special comparison like in this answer?
However, I don't want the performance hit of having to iterate through an entire list like that to compare every value.
Assuming it could ever be the same object in memory, is there some way to compare the "two" lists by asking "is it the same actual object in memory"?
That's exactly what
==
does. Take this example:Two different lists, albeit with the same contents, are not
==
, but can be compared by exhaustively checking their contents.But in this example
l3
is the same list asl1
- so==
is true. (Note how modifyingl1
also modifiesl3
.