Sobes.tech
Junior — Middle

How is an object implemented whose properties cannot be changed after its creation?

sobes.tech AI

Answer from AI

In Flutter (Dart), to create an object with immutable properties, the final keyword is usually used for class fields. This means that the property value can only be set once — at the time of object creation. You can also make a class with a constructor that initializes all fields and does not provide setters.

Example:

class User {
  final String name;
  final int age;

  User(this.name, this.age);
}

void main() {
  var user = User('Alice', 30);
  // user.name = 'Bob'; // Error, cannot change
}

If a completely immutable object is needed, a const constructor can be used, but for this, all fields must be final and have constant values.

How is an object implemented whose properties cannot… - sobes.tech