DEV Community

Cover image for Constructors in Dart
Jay Tillu
Jay Tillu

Posted on • Edited on

Constructors in Dart

  • A constructor is a special method (function) of a class that helps to create an object. As its name indicates its constructs the new object.

  • It helps to create the object by initializing values to instance variables. So we can say that its main goal is to provide values to instance variables.

How constructor is different from other methods of Class


There are some major differences between the normal method and constructor.

  1. The constructor has the same name as the class.
  2. The constructor doesn’t have a return type.
  3. A constructor is automatically called when the object is created.
  4. If we don’t specify a constructor, the default no-argument constructor will be created.

Syntax of declaring constructor

class_name (arguments) {  //If there is a block of code.
  // Constructor body
}

or

class_name (arguments); // If there is no block of code.
Enter fullscreen mode Exit fullscreen mode

Sample Code

class Student{
  Student(int enNum){
    print(enNum);
  }
}

main(){
  var myStudent = new Student(15);
}

Output
15
Enter fullscreen mode Exit fullscreen mode

Named Constructor


  • After seeing the advantages of the constructor you might want to create multiple constructors. But as we know constructor holds the name of the class. So, in that case, you might ask then how can we create multiple constructors and treat them differently. Then here comes a Named Constructor in the picture.

  • By using named constructor you can create multiple constructors in the same class. Each constructor will have a unique name. So that you can identify each of them.

Syntax of defining a named constructor

class_name.constructor_name (arguments){ 
   // If there is block of code use this syntax
   // Statements
}

or

class_name.constructor_name (arguments); 
   // If there is not block of code use this syntax
Enter fullscreen mode Exit fullscreen mode

Sample Code

class Employee {
  int empID;
  String empName;
  String empDept;

  Employee.ID(this.empID); // Named Constructor Creation

  Employee.name(this.empName);

  Employee.department(this.empDept);
}

main() {
  var myEmployee01 = new Employee.ID(15);
  var myEmployee02 = new Employee.department("Testing");
  var myEmployee03 = new Employee.name("Ashu");

  print(myEmployee01.empID);
  print(myEmployee02.empDept);
  print(myEmployee03.empName);
}

Output
15
Testing
Ashu
Enter fullscreen mode Exit fullscreen mode

So, guys, that’s it for constructors in a dart. Again this is also the core concept of Object Oriented Programming. So practice it a little bit. Please feel free to share with me if I miss something. Till Then Keep Loving, Keep Coding. And I surely catch you up in the next article.

Remember no teacher, no book, no video tutorial, or no blog can teach you everything. As one said Learning is Journey and Journey never ends. Just collect some data from here and there, read it, learn it, practice it, and try to apply it. Don’t feel hesitate that you can’t do that or you don’t know this concept or that concept. Remember every programmer was passed from the path on which you are walking right now. Remember Every Master was Once a Beginner. Work hard and Give your best.

Learn more about Dart and Flutter

Follow me for more such content

Top comments (0)