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.
class Configuration {
  String environment;
  int timeout;

  // Target unnamed constructor
  Configuration(this.environment, this.timeout);

  // Target named constructor
  Configuration.custom(this.environment, this.timeout);

  // Redirecting to the unnamed constructor
  Configuration.dev(String env) : this(env, 3000);

  // Redirecting to a named constructor
  Configuration.prod(String env) : this.custom(env, 5000);
}

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).
class Point {
  double x;
  double y;

  Point(this.x, this.y);

  // VALID: Terminates with a semicolon and is the exclusive initializer
  Point.xAxis(double x) : this(x, 0.0);

  // INVALID: Cannot have a body
  // Point.yAxis(double y) : this(0.0, y) { print('Created'); } 

  // INVALID: Cannot mix redirection with field initialization
  // Point.diagonal(double val) : x = val, this(val, val); 
}

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.
class ImmutablePoint {
  final double x;
  final double y;

  // Constant target constructor
  const ImmutablePoint(this.x, this.y);

  // Constant redirecting constructor passing its own parameter
  const ImmutablePoint.xAxis(double x) : this(x, 0.0);

  // Constant redirecting constructor passing literal constants
  const ImmutablePoint.origin() : this(0.0, 0.0);
}

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.
abstract class Logger {
  // Redirecting factory constructor
  factory Logger(String name) = ConsoleLogger;
}

class ConsoleLogger implements Logger {
  final String name;

  // Target constructor for the factory redirection
  ConsoleLogger(this.name);
}
Tired of Poor Dart Skills? Fix That With Deep Grasping!Learn More