Encapsulation in Dart

Encapsulation in Dart

Encapsulation

Encapsulation is the principle that limits access to of an object's state; and the bundling of methods and operations that do work on a set of data.

In other languages such as C++ and Java, it is common to control access to a field by making it private and having public getters and setters for that field. In Dart, fields and getters/setter are indistinguishable.

Minimal Encapsulation

The following example has minimal encapsulation. You can directly access its fields freely.

class Employee {
  String name;
}

void main() {
  var myEmployee = Employee();
  myEmployee.name = "Bobby";
}

Private Fields

If you want to restrict access to a certain field outside of a class, you can make it private by prefixing the field name with an underscore.

class Employee {
  String name;
  int _salary; // Private - This field cannot be accessed outside of this class
}

Read-only fields

If you want read-only (get-only) fields, simply add the final keyword.

class Employee {
  final String name;
}

Setters and Getters

If we want to have more control over the field, we can use the "private field, public getter/setter" pattern.

For example:

  • let's say we want to store the name in lowercase
  • and, we want to display it with only the first letter capitalized.

We can do something like this:

String titleCase(String string) => "${string[0].toUpperCase()}${string.substring(1)}";

class Employee {
  String _name;
  String get name => titleCase(_name);
  set name(String newValue) { _name = newValue.toLowerCase(); }
}

void main() {
  var myEmployee = Employee();
  myEmployee.name = "BOBBY";
  print(myEmployee.name); // Bobby
}

Did you find this article valuable?

Support Jethro Lorenzo Garcia Lising by becoming a sponsor. Any amount is appreciated!