Intermediate Dart
  • Welcome to the Course
  • Course Overview
  • Resources
  • Dart Fundamentals-2
    • Section Overview
    • Basic Concepts
      • Class
      • Object/Instance
      • Instance /Field Variables
      • Methods
      • "this" keyword-1
    • Constructor
      • Default Constructor
      • Parameterised Constructor
        • Basic Parameterised Constructor & "this" keyword -2
        • Parameterised Constructor with Syntactic sugar
        • Optional Arguments in Parameterised Constructor
      • Named Constructor
    • Getters & Setters and Encapsulation
      • Access Modifiers and _ symbol
      • Implicit / Default Getters & Setters
      • Custom Getters and Setters
      • Encapsulation
    • Inheritance
      • Inheritance, Types of Inheritance , Terminology
      • Method Overriding
      • @override annotation
      • super Keyword -1
      • super Keyword -2
    • Polymorphism
    • Static Variables and Methods
    • Abstract Class and Abstract Methods
    • Interface
    • Miscellaneous concepts
      • Untitled
      • Object Class
      • Mixin
      • Immutable Instances
      • Cascade notation
  • Dart Fundamentals -3
    • Section Overview
    • Asynchronous Programming
      • Basic Concept of Asynchronous programming
      • async , await , then
      • Exception Handling in asynchronous code
    • Functional Programming
      • Anonymous functions
      • High Level Iterators
    • Advanced Asynchronous Programming
      • Isolates
      • Streams
      • Generators
      • Microtask and Zones
    • Method & Constructor Overloading
Powered by GitBook
On this page

Was this helpful?

  1. Dart Fundamentals-2
  2. Getters & Setters and Encapsulation

Custom Getters and Setters

PreviousImplicit / Default Getters & SettersNextEncapsulation

Last updated 4 years ago

Was this helpful?

Pre-calculation example

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; 
  
  }
  
  
  
  
  
  
}


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; 
  
  }
  
  
  
  
  
  
}
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;
  
   
  
  
  
  
  
}
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;
  
   
  
  
  
  
  
}

I mentioned you can remove the return type from setter method without any problem, but

you can actually also remove return type from getter method also without any problem . Dart compiler can assume the return type

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;
}

Pre checking example

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; 
  
  }
 
}

You can also use custom getters and setters as an "alias" for the original instance variables.