Dart Getter Method
Dart Getter Method
In this tutorial, we will learn about the Dart getter method. The getter method reads the variable’s value or retrieves the object’s property.
Syntax
We can define the getters method by using the get keyword with no parameter and valid return type.
The general syntax to define a getter method is as follows:
return_type get getter_name {
return field_name;
}
Example
Let’s define a getter method in the following example:
/*
Define a getter method
Dart Tutorials – www.TestingDocs.com
*/
class Car {
String make;
String model;
int year;
String color;
//Getter method
String get carColor {
return color;
}
// Constructor
Car({required this.make, required this.model, required this.year, required this.color});
}
// main method
void main(){
Car car=
Car(make:”Toyota”,model:”Sedan”,year:2018,color:”Blue”);
print(“Car company : ${car.make}”);
print(“Car model : ${car.model}”);
print(“Car color : ” + car.carColor);
}
Dart Tutorials
Dart tutorial on this website can be found at:
https://www.testingdocs.com/dart-tutorials-for-beginners/