Getters and Setters in Dart and Flutter.
As in many other languages, getters \ setters are methods that give read / write access to object properties.
In this article, we’ll look at simple examples and run them in dartpad.
In Dart, reading and writing any properties of objects is implemented using getters \ setters.
For example, look at the code
class Person { String name; int birthYear; Person(this.name, this.birthYear); } void main() { Person person = Person('username', 1990); print('Hello ${person.name}, you was born in ${person.birthYear}'); person.name = 'Newusername'; print('Hello ${person.name}, you was born in ${person.birthYear}'); }
Each property in this class has an implicit setter to write to it and an implicit getter to get the value.
When we call person.name = 'Newusername';
then we turn to the setter class. And after that we get its value person.name
using getter.
In Dart, we can (and this is recommended to do for code abstraction) create our own getters and setters. This allows us to initialize our classes with properties, and in the future to wrap them with different methods without changing the client code.
For example, let's say our project with the Person class has evolved and we needed to determine the age of majority. We can do this without changing the constructor and basic properties of the object.
class Person { String name; int birthYear; bool get isAdult => (DateTime.now().year - birthYear) > 18; Person(this.name, this.birthYear); } void main() { Person personAdult = Person('adultUser', 1990); print('Hello ${personAdult.name}, you was born in ${personAdult.birthYear}, you are ${personAdult.isAdult ? 'adult' : 'not adult'}'); Person personNotAdult = Person('adultUser', 2005); print('Hello ${personNotAdult.name}, you was born in ${personNotAdult.birthYear}, you are ${personNotAdult.isAdult ? 'adult' : 'not adult'}'); }
Running the code in Dartpad, we see that we have added a new class behavior, while the already created client code will not be affected and everything will continue to work.
In the same way, if in some part of the project we started working with age, and not with the date of birth, then we can add setter to record the birthday when indicating age.
class Person { String name; int birthYear; bool get isAdult => (DateTime.now().year - birthYear) > 18; int get age => (DateTime.now().year - birthYear); set age(int val) => birthYear = (DateTime.now().year - val); Person(this.name, this.birthYear); } void main() { Person personAdult = Person('adultUser', 1990); print('Hello ${personAdult.name}, you was born in ${personAdult.birthYear}, you are ${personAdult.isAdult ? 'adult' : 'not adult'}'); Person personNotAdult = Person('adultUser', 2005); print('Hello ${personNotAdult.name}, you was born in ${personNotAdult.birthYear}, you are ${personNotAdult.isAdult ? 'adult' : 'not adult'}'); //check how setter work print(personAdult.birthYear); personAdult.age = 5; print(personAdult.birthYear); }
In general, getter & setter is used so often that Android AutoCraft is built into Android Studio. When the cursor on the property is pressed Command + N (in Windows Ctrl + N) and the context menu will create them.
Good coding to everyone!