Dart Getters and Setters
Dart Getters and Setters
A getter is a special method that reads a value.
A setter is a special method that changes a value.
From the outside, getters and setters look like normal fields.
You use them without parentheses ().
You create a getter with the get keyword:
Example
class Person {
String firstName;
String lastName;
Person(this.firstName, this.lastName);
String get fullName {
return '$firstName $lastName';
}
}
void main() {
var person = Person('John', 'Doe');
print(person.fullName);
}
Try it Yourself »
Result:
John Doe
Notice that we wrote person.fullName, not person.fullName().
Computed Properties
A getter does not store a value.
It computes the value every time you read it.
This is called a computed property.
A short getter is often written with the arrow syntax =>:
Example
class Rectangle {
double width;
double height;
Rectangle(this.width, this.height);
double get area => width * height;
}
void main() {
var rect = Rectangle(4, 5);
print(rect.area);
rect.width = 10;
print(rect.area);
}
Try it Yourself »
Result:
20.0
50.0
When the width changed, the area changed too, because it is computed again.
Private Fields
A name that starts with an underscore _ is private.
In Dart, private means private to the library.
A library is usually one .dart file.
Code in the same file can use a private name, but code in other files cannot.
A common pattern is a private field with a public getter.
Other files can read the value, but they cannot change it:
Example
class BankAccount {
double _balance = 0;
double get balance => _balance;
void deposit(double amount) {
_balance += amount;
}
}
void main() {
var account = BankAccount();
account.deposit(100);
account.deposit(50);
print(account.balance);
}
Try it Yourself »
Result:
150.0
Note: Dart has no private or public keywords.
The underscore at the start of the name is the only thing that makes it private.
There is no setter for balance, so you get an error if you try to assign to it:
Example
class BankAccount {
double _balance = 0;
double get balance => _balance;
}
void main() {
var account = BankAccount();
account.balance = 1000;
}
Try it Yourself »
Result:
main.dart:9:11: Error: The setter 'balance' isn't defined for the type 'BankAccount'.
- 'BankAccount' is from 'main.dart'.
Try correcting the name to the name of an existing setter, or defining a setter or field named 'balance'.
account.balance = 1000;
^^^^^^^
Setters
You create a setter with the set keyword.
A setter has exactly one parameter: the new value.
You call it with the normal assignment operator =:
Example
class Temperature {
double celsius = 0;
double get fahrenheit => celsius * 9 / 5 + 32;
set fahrenheit(double value) {
celsius = (value - 32) * 5 / 9;
}
}
void main() {
var temp = Temperature();
temp.fahrenheit = 212;
print(temp.celsius);
print(temp.fahrenheit);
}
Try it Yourself »
Result:
100.0
212.0
The getter and the setter have the same name, fahrenheit.
Check a Value in a Setter
A setter is a good place to check a value before you store it.
This is called validation.
In this example, the age is only changed if the new value is not negative:
Example
class Person {
String name;
int _age = 0;
Person(this.name);
int get age => _age;
set age(int value) {
if (value < 0) {
print('Age cannot be negative');
} else {
_age = value;
}
}
}
void main() {
var person = Person('Linus');
person.age = 30;
print(person.age);
person.age = -5;
print(person.age);
}
Try it Yourself »
Result:
30
Age cannot be negative
30
Tip: Only write getters and setters when they do something useful.
If a getter and a setter only read and write a field, a public field is simpler.