67

I am struggling with the concept of getters and setters in Dart, and the more I read, the more I cannot grasp the underlying purpose. Take for example the following code:

main() {
    Car car = new Car();
    car.doors = 44;
    print(car.doors);  // 44
}
class Car {
    int doors = 4;
}

Later, I decide to make “doors” a private variable, so I do the following:

main() {
    Car car = new Car();
    car.doors = 44;
    print(car.doors);  // 44
}
class Car {
    int _doors = 4;
    int get doors => _doors;
    set doors(int numberOfDoors) => _doors = numberOfDoors;
}

According to the code, _doors is now a private variable, and so I cannot access it in main(). However, by manipulating doors, I can indirectly change the value of _doors, which is what I thought I wanted to prevent in the first place by making it a private variable. So what is the purpose of making a previously public variable into a private one, if you can still indirectly manipulate it? And, how are getters and setters even working to change the properties of these variables? I am trying to understand the fundamental concept, because without that, I don't understand how or why getters and setters are used.

2 Answers 2

117

Instance variables in Dart have implicit getters and setters. So for your example code, it will operate in exactly the same way, since all you have done is changed from an implicit getter and setter to an explicit getter and setter.

The value of explicit getters and setters is that you don't need to define both if you don't want. For instance we can change your example to only define a getter:

main() {
    Car car = new Car();
    print(car.doors);  // 4
    car.doors = 6; // Won't work since no doors setter is defined
}

class Car {
    int _doors = 4;
    int get doors => _doors;
}

Additionally, you can also add extra logic in a getter or setter that you don't get in an implicit getter or setter:

class Car {
    int _doors = 4;
    int get doors => _doors;
    set doors(int numberOfDoors) {
      if(numberOfDoors >= 2 && numberOfDoors <= 6) {
        _doors = numberOfDoors;
      }
    }
}
3
  • 6
    I think the final point about additional logic is the KEY part. By making the variable private, you are requiring that any changes go through your setter, which enforces that the extra logic is run (one can consider the case of not defining a setter as a specific type of setter logic that prevents all setting). Dec 30, 2014 at 0:24
  • 7
    In summary, getters and setters are simply used to override the default getters and setters, allowing greater flexiblity. Situations where this can be useful: 1) when you want instance variables to have a different getter (reassign variable), setter (assign value), or both. 2) if you want to add logic into assigning values, setters can do this. In my example, I should delete the “set” line. That would reassign doors to equal the value of _doors (and not break backwards compatibility), but since doors cannot be changed, _doors will not be affected, making _doors truly private. Thank you.
    – richalot
    Dec 30, 2014 at 5:17
  • How can a setter be called within a constructor (e.g. so we can do something like myCar = Car(doors: 4)? Feb 24, 2020 at 5:29
12

The getter and setter functions allow us to make the class appear to have a property, without a explicit property being declared (_doors in your case). The property value may be calculated from other properties.

The getters and setters allow us to execute arbitrary code when the property is get or set.

Omitting a setter makes the property immutable.

An abstract class may declare getters and setters without bodies as part of a required class interface.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.