Immutability in Dart data classes is known as final
This class is immutable, why? After declaring the class you won't be able to change it's variables again.
class User {
final String name;
final int age;
const User(this.name, this.age);
}
Bonus:
If you want the other methods that are normally included automatically in a language such as toMap
, toJson
, fromJson
, fromMap
, toString
etc. You can also use code generation to get them with this extension:
https://marketplace.visualstudio.com/items?itemName=hzgood.dart-data-class-generator