Open In App

Dart - Getters and Setters

Last Updated : 28 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

Dart
// Dart program to illustrate the use
// of getters and setters with null safety

class Student {  
    
    // Fields with default values to avoid null errors
    String name = '';  
    int age = 0;   
    
    // Getter for the student's name  
    String get stud_name {  
        return name;  
    }  
    
    // Setter for the student's name  
    void set stud_name(String name) {  
        this.name = name;  
    }  
    
    // Setter for the student's age with validation  
    void set stud_age(int age) {  
        
        if (age <= 5) {  
            // Age should be greater than 5
            print("Age should be greater than 5");  
        } else {  
            this.age = age;  
        }  
    }  
    
    // Getter for the student's age  
    int get stud_age {  
        return age;      
    }  
}  
  
void main() {  
    // Creating an instance of the Student class  
    Student s1 = Student();  
    
    // Setting values using setters  
    s1.stud_name = 'GFG';  
    
    // Invalid age, should print an error message 
    s1.stud_age = 0;   
    
    // Getting values using getters  
    print(s1.stud_name);  // Output: GFG  
    print(s1.stud_age);   // Output: 0 (Default value)  
}


Output:

Age should be greater than 5
GFG
0


Getters and Setters in Action

Example :

Dart
// Dart program to illustrate getters and setters
void main() {
    // Creating an instance of Cat
    var cat = Cat();
    
    // Checking initial state: Is cat hungry? true
    print("Is cat hungry? ${cat.isHungry}");
    
    // Checking initial state: Is cat cuddly? false
    print("Is cat cuddly? ${cat.isCuddly}");  
    
    print("Feed cat.");
    
    // Setting isHungry to false (feeding the cat)
    cat.isHungry = false;   
    
    // Checking updated state: Is cat hungry? false
    print("Is cat hungry? ${cat.isHungry}");
    
    // Checking updated state: Is cat cuddly? true
    print("Is cat cuddly? ${cat.isCuddly}"); 
}

class Cat {
    // Private field to track hunger state
    bool _isHungry = true;
    
    // Getter: A cat is cuddly when it's not hungry
    bool get isCuddly => !_isHungry;
    
    // Getter: Returns the hunger state
    bool get isHungry => _isHungry;
    
    // Setter: Updates the hunger state
    set isHungry(bool hungry) {
        _isHungry = hungry;
    }
}


Output:

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



Next Article
Article Tags :

Similar Reads