Dart: Redirecting Constructor

Dart: Redirecting Constructor

ยท

1 min read

  • In simple terms when in one class one Constructor called to another Constructor then we call it Redirecting Constructor.

  • In this we use named constructor to call Parameterized constructor.

  • Let's understand with below example.

    • First we create one class called display and than pass to parameter which is var x and var y , then we create one Parameterized constructor called display pass this both parameter x and y using " this " keyword.

    • After that we created one named constructor and we create object of constructor not Parameterized constructor.


//Constant Constructor Constructor 
void main()
{
  var redirecting_Constructor = display.named();

}

class display
{
   var  x;
   var  y;

     display(this.x , this.y)
     {

        print("The value of x : ${x}");
        print("The value of y : ${y}");

     }

     display.named():this(20 , 40);
}

ย