Page 16 - Dart Language
P. 16
a getter method can be used:
class Foo {
String get bar {
var result;
// ...
return result;
}
}
Getters never take arguments, so the parentheses for the (empty) parameter list are omitted both
for declaring getters, as above, and for calling them, like so:
main() {
var foo = new Foo();
print(foo.bar); // prints "bar"
}
There are also setter methods, which must take exactly one argument:
class Foo {
String _bar;
String get bar => _bar;
void set bar(String value) {
_bar = value;
}
}
The syntax for calling a setter is the same as variable assignment:
main() {
var foo = new Foo();
foo.bar = "this is calling a setter method";
}
Constructors
A class constructor must have the same name as its class.
Let's create a constructor for a class Person:
class Person {
String name;
String gender;
int age;
Person(this.name, this.gender, this.age);
}
The example above is a simpler, better way of defining the constructor than the following way,
which is also possible:
https://riptutorial.com/ 11

