Dart Getters and Setters

 

Dart Getters and Setters

Getters and setters are the special class method that is used to read and write access to an object's properties. The getter method is used to reads the value of the variable or retrieve the value and setter method is used to set or initialize respective class fields. By default, all classes are associated with getter and setter method. However, we can override the default methods by defining getter and setter method explicitly.

Defining a getter

We can define the getters method by using the get keyword with no parameter a valid return type.

Syntax:

  1. return_type get field_name{  
  2. }  

Defining a setter

We can declare the setter method using the set keyword with one parameter and without return type.

Syntax:

  1. set field_name {  
  2. }  

Example:

  1. class Student {  
  2.           String stdName;  
  3.           String branch;  
  4.           int stdAge;  
  5.  // getter method   
  6.          String get std_name   
  7.              {  
  8.              return stdName;  
  9.              }  
  10.          void set std_name(String name)  
  11.                  {  
  12.               this.stdName = name;  
  13.   
  14.                }  
  15.           void set std_age(int age) {  
  16.                if(age> = 20){  
  17.                    print("Student age should be greater than 20")  
  18.                      }else{   
  19.                          this.stdAge = age;  
  20.                               }  
  21.                       }  
  22.   
  23.                                                          }  
  24.            int get std_age{  
  25.                  return stdAge;  
  26.   
  27. }  
  28.         void set std_branch(String branch_name) {  
  29.                  this.branch = branch_name;  
  30.   
  31. }  
  32.      int get std_branch{  
  33.             return branch;  
  34. }  
  35.   
  36. }  
  37. void main(){  
  38. Student std = new Student();  
  39. std.std_name = 'John';  
  40. std.std_age = 24;  
  41. std.std_branch = 'Computer Science';  
  42.   
  43. print("Student name is: ${std_name}");  
  44. print("Student age is: ${std_age}");  
  45. print("Student branch is: ${std_branch}");  
  46. }  

Output

Student name is: John

Student age is: 24
Student Branch is: Computer Science

We can also place the getter and setter method just after the. Now, let's understand the following example:

Example - 2

  1. class Car {  
  2.   String makedate;  
  3.   String modelname;  
  4.   int manufactureYear;  
  5.   int carAge;  
  6.   String color;  
  7. // Getter method  
  8.   int get age {  
  9.     return carAge;  
  10.   }  
  11. // Setter Method  
  12.   void set age(int currentYear) {  
  13.     carAge = currentYear - manufactureYear;  
  14.   }  
  15. // defining parameterized constructor  
  16.   Car({this.makedate,this.modelname,this.manufactureYear,this.color,});  
  17. }  
  18. //Age here is both a getter and a setter. Let's see how we can use it.  
  19. void main() {  
  20.  Car c =   
  21.  Car(makedate:"Renault 20/03/2010",modelname:"Duster",manufactureYear:2010,color:"White");  
  22.   print("The car company is: ${c.makedate}");   
  23.   print("The modelname is: ${c.modelname}");   
  24.   print("The color is:${c.color}");  
  25.   c.age = 2020;  
  26.   print(c.age);  
  27. }  

Output

The car company is: Honda 20/03/2010
The modelname is: City
The color is: White
10

Explanation:

In the above code, we defined the getter and setter methods before the constructor

Comments