Named Constructor

You can think of named constructors same as default constructors or parameterised constructors , but they have a name

You can have multiple named constructors inside the class definition

Named Constructor can also have optional argument as it's input , so it gives us a lot of flexibility for creating objects

void main() {
  
  var employee1 = Employee(1, "John", "Doe", "CTO", 12);
  
  var employee2 = Employee.mynamedconstructor2(2, "Jane", "Doe", "CE0", 10);
  
 

  print(employee2.id);
  print(employee2.firstname);
  print(employee2.lastname);
  print(employee2.position);
  print(employee2.number_of_day_worked);
}

class Employee {
  
  int id;

  String firstname;

  String lastname;

  String position;

  int number_of_day_worked;
  
  
  
  // Parameterised constructor 
  
Employee(this.id,this.firstname,this.lastname,this. position,this.number_of_day_worked);
  
  
  
  // Named constrcutor 
  
  
  Employee.mynamedconstructor(this.id,this.firstname,this.lastname,this. position,this.number_of_day_worked);
  
  
  
   Employee.mynamedconstructor2(this.id,this.firstname,this.lastname,this. position,this.number_of_day_worked);
  
  
  
  

}

Last updated