Member-only story
Dart Expert Trick For Immutable Value Type Models
The difference between keyword const and final is that const is compile-time and final is run-time, with both of those keywords giving us immutability. With const objects being compile-time that means that they are in computer memory and thus improve application performance. And we set a new value to use such things as models by using Object-Oriented-Programming, i.e. the class object copyWith method.
But, the devil is in the details as normally most Dart classes do not come with property equality methods set up for value type objects. And, some Class types require a different way to implement copyWith to get the proper value-type equality on top. But, with a little knowledge of dart internals, we can come up with some valid approaches to value type objects.
Dart’s Only Value Type Sealed Class
Now, for some Dart legacy history. We use to not have enums in Dart. In fact, the only way to get enums in Dart use to be that we had to use an outside library, namely Built Value. But Dart 2.0 eventually came out with enums implemented as an abstract sealed class even though we just use a keyword of enum, internally it’s an abstract EnumClass that is sealed which means that it can only be created once.