Understanding Getter and Setter in Dart

Understanding Getter and Setter in Dart

In object-oriented programming, encapsulation is a fundamental concept that promotes the idea of bundling data and methods that operate on the data within a single unit, known as a class. Encapsulation helps in hiding the internal state of an object and restricting direct access to it from outside the class. In Dart, a modern programming language developed by Google, getters and setters are key components for achieving encapsulation.

What are Getters and Setters?

In Dart, getters and setters are special methods used to control access to the attributes (fields) of a class. They provide a way to read and modify the private fields of a class, ensuring that access to these fields is performed through well-defined methods. This helps in maintaining the integrity of the object's state and allows for easier maintenance and modification of the class in the future.

Defining Getters and Setters

To define a getter or a setter in Dart, you use the get and set keywords respectively, followed by the name of the getter or setter. Let's take a look at how they are defined in Dart:

class MyClass {
  // Private field
  int _myField;

  // Getter
  int get myField => _myField;

  // Setter
  set myField(int value) {
    _myField = value;
  }
}

In the above example, myField is a private field of the class MyClass. The getter get myField allows us to access the value of _myField, while the setter set myField(int value) allows us to modify the value of _myField.

Using Getters and Setters

Now that we have defined getters and setters, let's see how they are used:

void main() {
  var obj = MyClass();

  // Using the getter
  print(obj.myField); // Output: null (if not initialized)

  // Using the setter
  obj.myField = 10;

  // Using the getter again
  print(obj.myField); // Output: 10
}

In the above example, we create an instance of MyClass named obj. We can access the private field _myField using the getter myField and modify it using the setter myField.

Advantages of Getters and Setters

  1. Encapsulation: Getters and setters allow us to control access to the private fields of a class, promoting encapsulation and information hiding.

  2. Validation: Setters provide an opportunity to validate the incoming data before assigning it to a field. This helps in maintaining the integrity of the object's state.

  3. Flexibility: Getters and setters provide a level of flexibility, allowing us to change the internal representation of data without affecting the external interface of the class.

Conclusion

Getters and setters are powerful tools in Dart that help in achieving encapsulation and maintaining the integrity of an object's state. By providing controlled access to the private fields of a class, they enable better organization, validation, and flexibility in managing data within an application. Understanding how to use getters and setters effectively is essential for writing clean, maintainable, and robust Dart code.

Did you find this article valuable?

Support Nikhil Soman Sahu by becoming a sponsor. Any amount is appreciated!