Constructors Design
Domains:
Dart
CONSIDER
CONSIDER making your constructor const
if the class supports it.
If you have a class where all the fields are final, and the constructor does nothing but initialize them, you can make that constructor const
. That lets users create instances of your class in places where constants are required—inside other larger constants, switch cases, default parameter values, etc.
If you don’t explicitly make it const
, they aren’t able to do that.
Note, however, that a const
constructor is a commitment in your public API. If you later change the constructor to non-const
, it will break users that are calling it in constant expressions. If you don’t want to commit to that, don’t make it const
. In practice, const
constructors are most useful for simple, immutable data record sorts of classes.