Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

An initializer list is a comma-separated sequence of expressions placed between a constructor’s parameter list and its body, prefixed by a colon (:). It executes before the constructor body runs and before the superclass constructor is invoked, allowing for the assignment of instance variables before the object is fully instantiated.
ClassName(parameterList) : initializer1, initializer2 {
  // constructor body
}

Execution Order

When a constructor is invoked, Dart processes the initialization phase in a strict sequence:
  1. Initializer list expressions (evaluated from left to right).
  2. Superclass constructor.
  3. Main class’s constructor body.

Scope and Restrictions

  • No this Access: The right-hand side of an initializer expression does not have access to the this keyword. You cannot invoke instance methods or read instance variables within the initializer list because the object is in an uninitialized state.
  • Final Variables: It is the primary mechanism for initializing final instance variables that require computation or parameter mapping before the constructor body executes.
  • Redirection Isolation: If an initializer list contains a redirecting constructor call (this(...)), it cannot contain any other initializers.

Syntax Mechanics

Variable Assignment and Assertions The list can contain direct assignments to instance variables and assert statements for parameter validation.
import 'dart:math';

class Vector {
  final double x;
  final double y;
  final double magnitude;

  Vector(double x, double y)
      : x = x,
        y = y,
        magnitude = sqrt(x * x + y * y),
        assert(x >= 0 && y >= 0) {
    // Body executes after x, y, and magnitude are set
  }
}
Superclass Delegation The initializer list is used to explicitly invoke a superclass constructor using super(...). If omitted, Dart implicitly calls the superclass’s unnamed, no-argument constructor.
class Vector3D extends Vector {
  final double z;

  Vector3D(double x, double y, double z)
      : z = z,
        super(x, y) {
    // Body executes after z is set and Vector(x, y) completes
  }
}
Constructor Redirection When delegating to another constructor within the same class, the initializer list consists solely of the this(...) call.
class Vector {
  final double x;
  final double y;

  Vector(this.x, this.y);

  // Redirecting constructor
  Vector.zero() : this(0.0, 0.0); 
}
Master Dart with Deep Grasping Methodology!Learn More