VOOZH about

URL: https://www.geeksforgeeks.org/dart/dart-getters-and-setters/

⇱ Dart - Getters and Setters - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Dart - Getters and Setters

Last Updated : 28 Mar, 2025

Getters and Setters, also called accessors and mutators, allow the program to initialize and retrieve the values of class fields respectively. 

  • Getters or accessors are defined using the get keyword.
  • Setters or mutators are defined using the set keyword.

A default getter/setter is associated with every class. However, the default ones can be overridden by explicitly defining a setter/getter. A getter has no parameters and returns a value, and the setter has one parameter and does not return a value.

Syntax for Getters and Setters

Defining a Getter

ReturnType get identifier { 
// statements
}

Defining a Setter

set identifier(Type value) { 
// statements
}

Using Getters and Setters in Dart

Example :

The following example shows how you can use getters and setters in a Dart class:


Output:

Age should be greater than 5
GFG
0


Getters and Setters in Action

Example :


Output:

Is cat hungry? true
Is cat cuddly? false
Feed cat.
Is cat hungry? false
Is cat cuddly? true


Comment
Article Tags:
Article Tags:

Explore