Sobes.tech
Junior — Middle

Kuidas realiseeritakse muutumatute omadustega objektide salvestamine?

sobes.tech AI

Vastus AI-lt

In Flutter (Dart), to store objects with immutable properties, classes with final fields (final) are commonly used. 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 = 'Petr'; // Compilation error, as name is final
}

You can also use packages like freezed or built_value, which help create immutable objects with convenient copy and comparison methods.