Skip to main content

Command Palette

Search for a command to run...

Dart: Redirecting Constructor

Published
1 min read
Dart: Redirecting Constructor

Sometimes a constructor's only purpose is to redirect to another constructor in the same class.

In simple words, inside in class when a constructor calls another constructor within the same class, we refer to this process as a Redirecting Constructor.

A redirecting constructor's body is empty with the constructor call appearing after a colon (:).

class Display{

    Display(this.x, this.y);//parameterized constructor
    Display.re_const(): this(2, 4);//named constructor

  • Display.named() : this(10, 20); is a redirecting constructor named named.

  • It doesn't take any parameters.

  • It redirects the construction process to the primary constructor by using constructor delegation this(10, 20).

  • When an object is created using this constructor, it immediately redirects to the primary constructor with values 10 and 20 for x and y, respectively.

Dart

Part 47 of 50

I'll be here to provide explanations, examples, and assistance as you explore the depths of Dart programming language. Together, we'll unravel the intricacies of Dart, from its fundamental concepts to

Up next

Dart: Constant Constructor

Define a Constant Constructor and make sure that all instance variable are final. Syntax: class Display{ final int x; const Display(this.x, ...); Basically, Constant Constructor have no body. Example: Here is the example using Constant Const...

More from this blog

Flutter Journey with Jinali

141 posts