VOOZH about

URL: https://www.geeksforgeeks.org/dart/constructors-in-dart/

⇱ Constructors in Dart - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Constructors in Dart

Last Updated : 2 Apr, 2025

Dart also provides the support of constructors. Constructors are a special method that is used to initialize fields when an object is 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 the class is called, moreover, one can also define a constructor of its own. But you must note that if you do so, then 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: 

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


Creating a constructor in Dart

Example 1:

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.

Creating a default constructor in Dart 

Example:


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 that. Those constructors that accept parameters constructors known as parameterized constructors

Creating a parameterized constructor in Dart 

Example :

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.

Declaring named constructors

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


Creating a Named Constructor in Dart 

Example :

Output:

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


Creating multiple constructors with the same name

Example :


Output:

compileDDC
main.dart:13:11: Error: String starting with ' must end with '.
print('Parameterized Constructor");
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
main.dart:13:10: Error: Can't find ')' to match '('.
print('Parameterized Constructor");
^
main.dart:12:3: Error: 'Gfg' is already declared in this scope.
Gfg(inta) {
^^^
main.dart:7:3: Context: Previous declaration of 'Gfg'.
Gfg() {
^^^
main.dart:13:11: Error: Expected ';' after this.
print('Parameterized Constructor");
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
main.dart:19:19: Error: Can't use 'Gfg' because it is declared more than once.
Gfg geek1 = new Gfg();
^
main.dart:20:19: Error: Can't use 'Gfg' because it is declared more than once.
Gfg geek2 = new Gfg(2);
^

This is the reason where named constructor concept was born.

Conclusion

Dart provides constructors for initializing object properties when an instance is created. Every class includes a default constructor unless a custom one is defined. Dart offers three types of constructors:

  • Default Constructor: This constructor takes no parameters and is automatically invoked when an object is created.
  • Parameterized Constructor: This type accepts arguments, allowing for custom initialization of object properties.
  • a This allows a class to have multiple constructors, which helps avoid name conflicts. Named constructors enhance flexibility, making object creation more efficient and organized.
Comment
Article Tags:

Explore