Dart Constructors

In this tutorial you will learn about the Dart Constructors and its application with practical example.

Dart Constructors

Constructor is a special method that is used to initialize an object when it is created. Constructor is called automatically when an object is instantiated; it is mainly used to set initial values for instance variables. The constructor has the same name as of class it belongs to. Constructor is syntactically similar to a instance method; but constructors have no explicit return type. It is not mandatory to write a constructor for a class. All classes have its own default constructor, if you don’t create any constructor for a class, the compiler will automatically creates a default constructor every class by assigning the default values to the member variables. But if you define your own constructor, the default constructor will be ignored.

Example:-

Lets say we have class by the name Employee, we will create object for Employee class as below –

This will invoke the default constructor of the Employee class.

Creating Constructors In Dart

A constructor has same name as that of the class, and doesn’t return any value. Suppose if we have class Test, then the constructors name should also be Test.

Syntax:-

There are two important rules to be kept in mind while creating a constructor.

  • The Constructor name should be the same name as the class name. Suppose if we have class Test, then the constructors name should also be Test.
  • The Constructor cannot have a explicit return type

Example:-

Output:-

Types of Constructors

There are following type of Constructors in Dart, they are

  • Default Constructor (or) no-arg Constructor
  • Parameterized Constructor
  • Named Constructor

Default Constructor (or) no-arg constructor

As the name specifies the constructor that has no parameter is known as default constructor. If you don’t create any constructor for a class, the compiler will automatically creates a default constructor(with no arguments) for the class. And if we create a constructor with no-arguments or arguments then the compiler will not create a default constructor. Default constructor provides the default values to the member variables.

Syntax:-

Example:-

Output:-

dart_default_constructor_with_no_arguments

Parameterized Constructor

Constructors can also take parameters, which is used to initialize instance variables. Most often, we will need a constructor that accepts one or more parameters. A constructor that accepts parameters is known as parameterized constructor. If we want to initialize instance variables with own values, then we are required to use a parameterized constructor.

Syntax:-

Example:-

Output:-

dart_parameterized _constructor_with_parameters

Named constructors

In Dart, named constructors allows a class to define multiple constructors.

Syntax:-

Example:-

Output:-

dart_named_constructor

In this tutorial we have learn about the Dart Constructors and its application with practical example. I hope you will like this tutorial.