Junior — Middle
How is storage of objects with immutable properties implemented?
sobes.tech AI
Answer from AI
In Flutter (Dart), classes with final fields (final) are commonly used to store objects with immutable properties. This means that after creating an instance of the object, the values of these fields cannot be changed. This approach ensures the immutability of the object.
Example:
class User {
final String name;
final int age;
User(this.name, this.age);
}
void main() {
final user = User('Ivan', 30);
// user.name = 'Peter'; // Compilation error, as name is final
}
You can also use packages like freezed or built_value, which help create immutable objects with convenient copying and comparison methods.