Skip to main content
A redirecting constructor is a constructor that delegates the instantiation process to another constructor within the same class. Instead of implementing its own initialization logic or body, it forwards its parameters—or computed default values—to a target constructor using the this keyword.

Syntax

A redirecting constructor is defined by appending a colon (:) followed by a call to this(...) for an unnamed target constructor, or this.constructorName(...) for a named target constructor.

Architectural Rules and Constraints

When defining a redirecting constructor, the Dart compiler enforces strict structural rules:
  1. No Block Body: A redirecting constructor cannot have a function body. It must terminate immediately with a semicolon after the delegation.
  2. Exclusive Initializer: The redirection must be the only item in the initialization list. You cannot combine this(...) with instance variable initializers, super(...) calls, or assert statements.
  3. Same-Class Delegation: The redirection must target a constructor within the exact same class. Delegating to a parent class constructor requires super, which is a standard initializer, not a redirecting constructor.
  4. Parameter Evaluation: The arguments passed to this(...) can be parameters received by the redirecting constructor, literal values, or expressions, provided those expressions do not reference this (the instance being created).

Constant Redirecting Constructors

If the target constructor is defined as a const constructor, the redirecting constructor can also be declared as const. The arguments passed in the redirection can be compile-time constants (like literals) or the parameters passed directly into the redirecting constructor itself.

Factory Redirecting Constructors

Dart also supports redirecting factory constructors. While standard redirecting constructors delegate to another constructor in the same class, a redirecting factory constructor delegates the creation of the object to a constructor of a different class (typically a concrete subclass). This is denoted using the = operator rather than a colon.
Tired of Poor Dart Skills? Fix That With Deep Grasping!Learn More