Constructor in Dart

Constructor In Dart

Constructor is the special function used for initializing the variables in dart. You can use it for setting the initial values for the variables when new object is created. It has no return type and only called once at the time of creating an object.

Keypoint About Constructor

  • The constructor’s name should be the same as the class name.
  • It can be parameterized.
  • Constructor doesn’t have any return type.
  • Constructor is only called once at the time of the object creation.
  • Dart supports three types of constructors: Default, Parameterized, and Named constructors.

Syntax

class ClassName {
  /*this section will contain various 
  attributes and methods associated with the current class*/

  ClassName() {
    /*this section will contains all the tasks that are performed
  on teh creation of the object*/
  }
}

Default Constructor

When no constructor is specified default constructor is generated, which will have no parameters.

Example Of Default Constructor In Dart

class Animal {
  var animalName;
  var numberOfLegs;
  var lifeSpan;

  Animal() {
    print('This is Default Constructor.');
  }

  void displayAnimalDetails() {
    print("Animal name: " + animalName);
    print("Number of Legs: " + numberOfLegs.toString());
    print("Life Span: " + lifeSpan);
  }
}

void main(List<String> args) {
  var animal = Animal();
  animal.animalName = "Cat";
  animal.numberOfLegs = 4;
  animal.lifeSpan = "12-18 years";

  animal.displayAnimalDetails();
}
Show Output

Parameterized Constructor

Parameterized constructor is the constructor that takes parameters. You pass parameters when creating an object that sets the initial values to the variables.

Example Of Parameterized Constructor In Dart

class Animal {
  var animalName;
  var numberOfLegs;
  var lifeSpan;

  Animal(var animalName, var numberOfLegs, var lifeSpan) {
    this.animalName = animalName;
    this.numberOfLegs = numberOfLegs;
    this.lifeSpan = lifeSpan;
  }

  void displayAnimalDetails() {
    print("Animal name: " + animalName);
    print("Number of Legs: " + numberOfLegs.toString());
    print("Life Span: " + lifeSpan);
  }
}

void main(List<String> args) {
  var animal = Animal("Cat", 4, "12-18 years");

  animal.displayAnimalDetails();
}
Show Output

At the time of object creation, you can pass the parameters through the constructor which initialize the variables value, avoiding the null values.

Named Constructor

It is impossible to have multiple parameterized constructors with the same name so that you can use the concept of named constructor in dart. Where parameters are set as required, can assign default values, or can make it optional.

Example Of Named Constructor In Dart

class Animal {
  var animalName;
  var numberOfLegs;
  var lifeSpan;

  Animal({this.animalName, this.numberOfLegs, this.lifeSpan});

  void displayAnimalDetails() {
    print("Animal name: " + animalName);
    print("Number of Legs: " + numberOfLegs.toString());
    print("Life Span: " + lifeSpan);
  }
}

void main(List<String> args) {
  var animal =
      Animal(animalName: "Cat", numberOfLegs: 4, lifeSpan: "12-18 years");

  animal.displayAnimalDetails();

}
Show Output

Keypoints About Named Constructor

  • You can directly pass the global variable on the parameter instead of creating a redundant local variable.
  • It is compulsory to write the variable name when passing value.
  • No need to follow the specified sequence.