> For the complete documentation index, see [llms.txt](https://eece.gitbook.io/mist-innovation-club-flutter-course-1/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://eece.gitbook.io/mist-innovation-club-flutter-course-1/dart-fundamentals-1/untitled/custom-getters-and-setters.md).

# Custom Getters and Setters

{% embed url="<https://youtu.be/zB56cBdWxu0>" %}

Pre-calculation example&#x20;

{% tabs %}
{% tab title="General syntax " %}

```dart
void main() {
  
  
  // Custom  Getters & Setters



  var employee1 = Employee(); // Creating employee1 object
  
 
  employee1.id =1;
  employee1.name ="John Doe";
  employee1.position ="CTO";
  employee1.empsalary = 30; // Calling the custom setter method 
  
  
  
  
  print(employee1.empsalary); // Calling the custom getter method 
  
  
}

class Employee {
  int id;

  String name;

  String position;
   
  int salary ; 

  
  
 // You can remove the return type from setter method
   void set empsalary(int dayworked) {
    
      this.salary = dayworked*20;    
  }
  
  
  
  int get empsalary{
  
    return this.salary; 
  
  }
  
  
  
  
  
  
}



```

{% endtab %}

{% tab title="Without "this" " %}

```dart
void main() {
  
  
  // Custom  Getters & Setters



  var employee1 = Employee(); // Creating employee1 object
  
 
  employee1.id =1;
  employee1.name ="John Doe";
  employee1.position ="CTO";
  employee1.empsalary = 30; // Calling the custom setter method 
  
  
  
  
  print(employee1.empsalary); // Calling the custom getter method 
  
  
}

class Employee {
  int id;

  String name;

  String position;
   
  int salary ; 

  
  
// You can remove the return type from setter method
 
   void set empsalary(int dayworked) {
    
      salary = dayworked*20;    
  }
  
  
  
  int get empsalary{
  
    return salary; 
  
  }
  
  
  
  
  
  
}
```

{% endtab %}

{% tab title="Shorthand notation" %}

```dart
void main() {
  
  
  // Custom  Getters & Setters



  var employee1 = Employee(); // Creating employee1 object
  
 
  employee1.id =1;
  employee1.name ="John Doe";
  employee1.position ="CTO";
  employee1.empsalary = 30; // Calling the custom setter method 
  
  
  
  
  print(employee1.empsalary); // Calling the custom getter method 
  
  
}

class Employee {
  int id;

  String name;

  String position;
   
  int salary ; 

  
  
 // You can remove the return type from setter method
   void set empsalary(int dayworked) => this.salary = dayworked*20;    
  
  
  
  
  int get empsalary => this.salary;
  
   
  
  
  
  
  
}
```

{% endtab %}

{% tab title="Shorthand+ without "this"" %}

```dart
void main() {
  
  
  // Custom  Getters & Setters



  var employee1 = Employee(); // Creating employee1 object
  
 
  employee1.id =1;
  employee1.name ="John Doe";
  employee1.position ="CTO";
  employee1.empsalary = 30; // Calling the custom setter method 
  
  
  
  
  print(employee1.empsalary); // Calling the custom getter method 
  
  
}

class Employee {
  int id;

  String name;

  String position;
   
  int salary ; 

  
  
   // You can remove the return type from setter method
   void set empsalary(int dayworked) => salary = dayworked*20;    
  
  
  
  
  int get empsalary => salary;
  
   
  
  
  
  
  
}
```

{% endtab %}
{% endtabs %}

> I mentioned you can remove the return type from setter method without any problem, but&#x20;
>
> you  can actually also **remove return type from getter method** **also** without any problem . Dart compiler can assume the return type&#x20;

{% tabs %}
{% tab title="Without any return type " %}

```dart
void main() {
  // Custom  Getters & Setters

  var employee1 = Employee(); // Creating employee1 object

  employee1.id = 1;
  employee1.name = "John Doe";
  employee1.position = "CTO";
  employee1.empsalary = 30; // Calling the custom setter method

  print(employee1.empsalary); // Calling the custom getter method
}

class Employee {
  int id;

  String name;

  String position;

  int salary;

  // You can remove the return type from setter method
  set empsalary(int dayworked) => salary = dayworked * 20;

  // You can remove the return type from getter method also 
  get empsalary => salary;
}


```

{% endtab %}
{% endtabs %}

Pre checking example&#x20;

```dart
void main() {
  
  
  // Custom  Getters & Setters



  var employee1 = Employee(); // Creating employee1 object
  
 
  employee1.id =1;
  employee1.name ="John Doe";
  employee1.position ="CTO";
  employee1.empsalary = 30; // Calling the custom setter method 
  
  
  
  
  print(employee1.empsalary); // Calling the custom getter method 
  
  
}

class Employee {
  int id;

  String name;

  String position;
   
  int salary ; 

  
  
 // You can remove the return type from setter method
   void set empsalary(int dayworked) {
     
     if(dayworked<=31)
     {
       this.salary = dayworked*20;    
       
     }
     
     else{
       
       
       this.salary = -1;    
       
       
     }
    
      
  }
  
  int get empsalary{
  
    return this.salary; 
  
  }
 
}
```

{% tabs %}
{% tab title="Custom Setter" %}
![](https://1972396024-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MCuqiBdVbVkT07RwbQD%2F-MD_r9CxfL7tYCeJU5Oh%2F-MD_rt2IJqa7BI0extPP%2Fsetter_final.svg?alt=media\&token=85a2fa23-cb98-4964-8e25-82365e6f1809)
{% endtab %}

{% tab title="Custom Getter" %}
![](https://1972396024-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MCuqiBdVbVkT07RwbQD%2F-MD_sbcq7gAS-Jt-XZot%2F-MD_sfTqxjQ-WrqnHsx9%2Fgetter_final_final.svg?alt=media\&token=12bedb88-f945-4508-8b25-9d7282125093)
{% endtab %}
{% endtabs %}

{% hint style="info" %}
You can  also use custom getters and setters as an "alias" for the original instance variables.
{% endhint %}
