Encapsulation in Dart

Encapsulation In Dart

Encapsulation is the mechanism that confirms critical and sensitive data which is hidden from users. To achieve encapsulation, you can make fields private and use the public getter and setter method to access and set the value of that field.

Minimal/Default Encapsulation

In dart, encapsulation happens at the library level and not at the class level. It provides a default getter and setter method so, you can directly get the value and set the value using that field name.

class Employee {
  var name;
}

void main() {
  var employee = Employee();
  employee.name = "Jack";
  print(employee.name);
}

Here, you can see that when the object of the class Employee is created and the object is stored in the variable name employee, then you use the default getter and setter method to access the fields within that class. Here, objectName.fieldName (employee.name ) is used to set the value “Jack” then objectName.fieldName (employee. name) to get the value and then printed it.

Private Fields

In dart, no public or private keywords are used. So, to make the variable private, underscore ( _ ) is used as a prefix to that field name. For E.g: If the field name is addressed to make it private it is written as _address.

class Employee {
  var _name;
}

void main() {
  var employee = Employee();
  employee._name = "Jack";
  print(employee._name);
}

Read-only Fields

By using the read-only field you can control the access of the field and implement the encapsulation in the dart. You can do that by adding the final keyword before the field declaration. Hence, you can only access its value but no new value can be set.

class Employee {
  final _name="Jack";
}

void main() {
  var employee = Employee();
  print(employee._name);
}

Custom Getters And Setters

Using the default getter, or setter with the private field cannot be always enough and you may want a specific kind of access to fields. In that case, you can use the custom getter and setters method.

For E.g: If you want to set the value in the uppercase letter, instead of using the default setter method, you can use the custom setter method which will set any passed value into uppercase and set the field value.

Info

Note: Use the get and set keyword to define the custom getter and setter method.

String titleCase(String string) {
  //Here you are using the concept of string interpolation; by prefixing the $ sign, variable 
  // can be directly accessed inside the string
  var titleCase = "${string[0].toUpperCase()}${string.substring(1)}";
  return titleCase;
}

class Employee {
  var _name;
  String get name => titleCase(_name);
  set name(String newValue) {
    //toLowerCase is predefined method
    _name = newValue.toLowerCase();
  }
}

void main() {
  var myEmployee = Employee();
  myEmployee.name = "JACK";
  print(myEmployee.name);
  myEmployee.name = "jack";
  print(myEmployee.name);
}
Show Output

Now, using the default getter and setter method you can now get a standardized value which is the title case name even if you pass the full uppercase value or lower case value. This can be used when some fields have a fixed set of formats. For Eg: In an email address, it doesn’t matter if you write it in uppercase or lowercase, which in the backend works something like this and converts it to the standard format using programming logic.