Flutter Tutorial: Getters & Setters

Encapsulation is one of the four pillars of Object Oriented Programming.

In this article, we will take a look at what this concept is and the practical implementation of the concept.

What to expect:

  1. Overview of Encapsulation.
  2. Setter: what it is and how to use it.
  3. Getter: what it is and how to use it.

Encapsulation

  • What is it?

Naturally, encapsulation is a way for us to prevent external manipulation of our object's properties, what I mean by this, is that if we want to prevent functions/methods outside an object's class from having direct access to a property of our object we can simply hide it from them and still provide them with the property's value accordingly.

  • Why do we need it?

Let's say for example you wanted to make some coffee, first, you add the coffee beans, then you add fresh cold water, and finally, you start your coffee maker. Your coffee maker brews the coffee then the freshly brewed coffee makes its way into your coffee pot. Little wonder what happened to the coffee beans, you see during brewing the caffeine which you need is extracted and dispensed into the coffee pot while the excesses are left in the chamber as coffee grounds, to be discarded later. This is basically how encapsulation works, it allows us to update or retrieve our property's value in a way that is appropriate to our object.

  • Case Study A Timer program. Our timer has two properties, Minute, and Second and they are both of the type ints.
class Timer {

  int minute = 0;

  int second = 0;

}

Normally, this would be fine, but this can cause bugs in our program. The problem with this class is its properties, since they are all ints, then they can accept any value as long as it's an integer, but since we know that second should only go as high as 59 with zero included, I hope we can now see the problem. To avoid this problem we use setters.

Setter

  • What is it?

A setter method is a function that is used to set or modify the value of an object's private property. It is also called "mutator", basically it is a method that grants write access to an instance's variable, meaning that it is only through this method that we can access our property's value in order to make changes to it.

  • How does it work?

First, we make our property private.

class Timer {

  int minute = 0;

  int _second = 0;

}

Then we add a setter method

set second(int second){

    if(second >= 0 && second <= 59){

      _second = second;

    }     

  }

Now what we have done is, we have hidden our "second" property from the rest of the program, and it can now only be accessed within the Timer class, and then we have created a setter method that handles the updating of its values based on the logic that the "second" property cannot be lower than zero or higher than 59, the question is if our property is now private how do we access the value when we need to. That is where getters come in.

Getter

  • What is it?

A getter method is a function that is used to read or access the value of an object's private property. It is also called "accessor", basically, it is a method that grants read-only access to a property, meaning that it is only through this method that we can read our property's value. It is pretty much the opposite of a setter.

  • How does it work?

Now that our property is private, we simply write the method to retrieve its value

int get second => _second;

And now we have completed a good old fashion encapsulation of our "second" property.

Note

  1. Although in programming languages such as Java the convention is to prefix the name of getters and setters with "set" and "get", this is because calling a getter method is different from accessing a field in Java, however, it is not necessary for dart, in fact, getters and setters are allowed to have the same name. In dart Fields and getters/setters are completely indistinguishable.

  2. Do not use getters and setters if your property isn't declared as private, this is redundant since every variable by default has a setter and getter, so you are basically overriding those methods.

  3. Do not use a setter for a "final" property. Unlike other variables, final variables do not have a setter method by default, and they shouldn't since we only have to initialize them once, so since there is nothing to override this would throw an error.

  4. Only use a setter if it's required because you want to execute some additional code aside from just forwarding the value directly to a field.

Conclusion

OOP is one of the biggest topics in programming and its values cannot be overstated, one of the four pillars of OOP is Encapsulation. In this article, I explained Encapsulation, and why we need it. I also explained getters and setters, and their practical implementation. Finally, I gave notes which are the basic don'ts of using getters and setters in the dart language. I hope you enjoyed the article and most importantly learned from it. Till next time