Getter in Dart

Create Free Backend With Appwrite

Getter In Dart

Getter is used to get the value of a property. It is mostly used to access a private property’s value. Getter provide explicit read access to an object properties.

Syntax

return_type get property_name {
  // Getter body
}
Info

Note: Instead of writing { } after the property name, you can also write => (fat arrow) after the property name.

Example 1: Getter In Dart

In this example below, there is a class named Person. The class has two properties firstName and lastName. There is getter fullName which is responsible to get full name of person.

class Person {
  // Properties
  String? firstName;
  String? lastName;

  // Constructor
  Person(this.firstName, this.lastName);

  // Getter
  String get fullName => "$firstName $lastName";
}

void main() {
  Person p = Person("John", "Doe");
  print(p.fullName);
}

Show Output
Run Online

Example 2: Getter In Dart

In this example below, there is a class named NoteBook. The class has two private properties _name and _prize. There are two getters name and price to access the value of the properties.

class NoteBook {
  // Private properties
  String? _name;
  double? _prize;

  // Constructor
  NoteBook(this._name, this._prize);

  // Getter method to access private property _name
  String get name => this._name!;

  // Getter method to access private property _prize
  double get price => this._prize!;
}

void main() {
  // Create an object of NoteBook class
  NoteBook nb = new NoteBook("Dell", 500);
  // Display the values of the object
  print(nb.name);
  print(nb.price);
}
Show Output
Run Online
Info

Note: In the above example, a getter name and price are used to access the value of the properties _name and _prize.

Example 3: Getter In Dart With Data Validation

In this example below, there is a class named NoteBook. The class has two private properties _name and _prize. There are two getters name and price to access the value of the properties. If you provide a blank name, then it will return No Name.

class NoteBook {
  // Private properties
  String _name;
  double _prize;

  // Constructor
  NoteBook(this._name, this._prize);

  // Getter to access private property _name
  String get name {
    if (_name == "") {
      return "No Name";
    }
    return this._name;
  }

  // Getter to access private property _prize
  double get price {
    return this._prize;
  }
}

void main() {
  // Create an object of NoteBook class
  NoteBook nb = new NoteBook("Apple", 1000);
  print("First Notebook name: ${nb.name}");
  print("First Notebook price: ${nb.price}");
  NoteBook nb2 = new NoteBook("", 500);
  print("Second Notebook name: ${nb2.name}");
  print("Second Notebook price: ${nb2.price}");
}

Show Output
Run Online

Example 4: Getter In Dart

In this example below, there is a class named Doctor. The class has three private properties _name, _age and _gender. There are three getters name, age, and gender to access the value of the properties. It has map getter to get Map of the object.

class Doctor {
// Private properties
  String _name;
  int _age;
  String _gender;

// Constructor
  Doctor(this._name, this._age, this._gender);

// Getters
  String get name => _name;
  int get age => _age;
  String get gender => _gender;

// Map Getter
  Map<String, dynamic> get map {
    return {"name": _name, "age": _age, "gender": _gender};
  }
}

void main() {
// Create an object of Doctor class
  Doctor d = Doctor("John", 41, "Male");
  print(d.map);
}

Show Output

Run Online

Why Is Getter Important In Dart?

  • To access the value of private property.
  • To restrict the access of data members of a class.

Video

Watch our video on getters in Dart.

Conclusion

In this section, you have learned about Getter with the help of examples. In the next section, you will learn about Setter In Dart.