Open In App

Constructors in Dart

Improve
Improve
Like Article
Like
Save
Share
Report

Dart also provides the support of constructors. Constructors are a special method that is used to initialize an object when created in the program. In object-oriented programming when an object is created, it automatically calls the constructor. All classes have their default constructor which is created by the compiler when class is called, moreover one can also define constructor of its own. But, you must note that if you do so than the default constructor will not be created and will be ignored.
 

Constructors in Dart: The constructors have the same name as the class name and don’t have any return type. 

class_name( [ parameters ] ){
    // Constructor Body
}

In the above syntax: 

  1. class_name is the name of the class whose constructor is being created.
  2. parameters are optional features and they can and can’t be defined for the constructor. The default constructor has no parameter defined in it.
  3. Constructor body is the body of the constructor and is executed when the constructor is called i.e when an object is created.
  4. Constructors don’t have any return type.

Example 1: Creating a constructor in Dart 
 

Dart




// Dart Program to create a constructor
 
// Creating Class named Gfg
class Gfg{
   
  // Creating Constructor
  Gfg() {
     
    // Whenever constructor is called
    // this statement will run
    print('Constructor is being created');
  }
   
  // Creating Field inside the class
  String geek1;
   
  // Creating Function inside class
  void geek(){
    print("Welcome to $geek1");
  }
}
 
void main() {
   
  // Creating Instance of class
  Gfg geek = new Gfg();
   
  // Calling field name geek1
  // and assigning value to it
  // using object of the class Gfg
  geek.geek1 = 'GeeksforGeeks';
   
  // Calling function name
  // geek using object
  // of the class Gfg
  geek.geek();
}


 

Output: 

Constructor is being created
Welcome to GeeksforGeeks

There are three types of constructors in Dart: 

1. Default Constructor: The default constructors are those constructors that don’t have any parameters in it. Thus, if a constructor which don’t have any parameter then it will be a type of default constructor.

Example: Creating default constructor in Dart 
 

Dart




// Dart program to illustrate
// the Default constructor
 
// Creating Class named Gfg
class Gfg{
   
  // Creating Constructor
  Gfg() {
    print('This is the default constructor');
  }
}
 
void main() {
   
  // Creating Instance of class
  Gfg geek = new Gfg(); 
}


 

Output: 

This is the default constructor

2. Parameterized Constructor: In Dart, you can also create a constructor having some parameters. These parameters will decide which constructor will be called and which will be not. Those constructors which accept parameters is known as parameterized constructor
 

Example:  
 

Dart




// Creating parameterized constructor in Dart
 
// Creating Class named Gfg
class Gfg{
   
  // Creating Parameterized Constructor
  Gfg(int a) {
     
    print('This is the parameterized constructor');
  }
}
 
void main() {
   
  // Creating Instance of class
  Gfg geek = new Gfg(1); 
}


 

Output: 

This is the parameterized constructor

Note: You can’t have two constructors with the same name although they have different parameters. The compiler will display an error.
 

3. Named Constructor: As you can’t define multiple constructors with the same name, this type of constructor is the solution to the problem. They allow the user to make multiple constructors with a different name.

class_name.constructor_name ( parameters ){
   // Body of Constructor
}

Example: 1
 

Dart




// Creating named constructor in Dart 
 
// Creating Class named Gfg
class Gfg{
   
  // Creating named and
  // parameterized Constructor
  // with one parameter
  Gfg.constructor1(int a) {
    print('This is the parameterized constructor with only one parameter');
  }
  // Creating named and
  // parameterized Constructor
  // with two parameter
  Gfg.constructor2(int a, int b) {
    print('This is the parameterized constructor with two parameters');
    print('Value of a + b is ${a + b}');
  }
}
 
void main() {
  // Creating Instance of class
  Gfg geek1 = new Gfg.constructor1(1);
  Gfg geek2 = new Gfg.constructor2(2, 3);
}


 

Output: 

This is the parameterized constructor with only one parameter
This is the parameterized constructor with two parameters
Value of a + b is 5

Example 2: You can see how dart behave with multiple constructor with same name

Dart




// Creating multiple constructor in Dart 
 
// Creating Class named Gfg
class Gfg{
   
  // Creating default constructor
  Gfg() {
    print('Default Constructor');
  }
   
  // Creating parameterized Constructor
  Gfg(int a) {
    print('Parameterized Constructor");
  }
}
 
void main() {
  // Creating Instance of class
  Gfg geek1 = new Gfg.constructor1();
  Gfg geek2 = new Gfg.constructor2(2);
}


Output:

program01.dart:13:3: Error: 'Gfg' is already declared in this scope.
Gfg(int a) {
^^^
program01.dart:8:3: Context: Previous declaration of 'Gfg'.
Gfg() {
^^^
program01.dart:21:23: Error: Couldn't find constructor 'Gfg.constructor1'.
Gfg geek1 = new Gfg.constructor1();
^^^^^^^^^^^^
program01.dart:22:23: Error: Couldn't find constructor 'Gfg.constructor2'.
Gfg geek2 = new Gfg.constructor2(2);

This is the reason where named constructor concept was born.



Last Updated : 06 Oct, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads