Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do getters and setters change properties in Dart?

Tags:

dart

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.

like image 960
richalot Avatar asked Dec 29 '14 05:12

richalot


People also ask

How does getter setter work in darts?

Getter and setter methods are the class methods used to manipulate the data of the class fields. Getter is used to read or get the data of the class field whereas setter is used to set the data of the class field to some variable.

Are getters and setters necessary in Dart?

Reading and writing access to objects is very important in any programming language. Getter and Setter are the exact methods that we use when we want to access the reading and writing privileges to an object's properties.

What is getter and setter properties?

What are Getters and Setters? Getters: These are the methods used in Object-Oriented Programming (OOPS) which helps to access the private attributes from a class. Setters: These are the methods used in OOPS feature which helps to set the value to private attributes in a class.


1 Answers

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;       }     } } 
like image 51
Pixel Elephant Avatar answered Sep 26 '22 00:09

Pixel Elephant