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.

A constructor in Dart is a special function invoked during the instantiation of a class, strictly responsible for initializing the newly created object’s state. Memory allocation is handled by the Dart runtime prior to the constructor’s execution. Unlike regular methods, constructors share the same name as the class (or a specific named variant) and do not declare a return type. If no constructor is explicitly declared, Dart implicitly provides a default, no-argument generative constructor. A fundamental rule in Dart is that constructors are not inherited; subclasses do not inherit generative or named constructors from their superclass.

Generative Constructors and Initializing Formal Parameters

The most common type of constructor is the generative constructor. Dart provides syntactic sugar called initializing formal parameters (this.propertyName) to assign arguments directly to instance variables before the constructor body executes.
class Point {
  double x;
  double y;

  // Generative constructor using initializing formal parameters
  Point(this.x, this.y);
}

Named Constructors

Dart does not support constructor overloading (multiple constructors with the same name but different signatures). Instead, Dart utilizes named constructors to allow multiple instantiation patterns for a single class.
class Point {
  double x;
  double y;

  Point(this.x, this.y);

  // Named constructor syntax: ClassName.identifier
  Point.origin() 
      : x = 0, 
        y = 0;
}

Initializer Lists

An initializer list executes after arguments are evaluated but immediately before the constructor body runs. It is separated from the constructor signature by a colon (:). It is commonly used to initialize final fields that are not initialized at their declaration site or via formal parameters (though late final fields can be initialized inside the constructor body). It is also required when passing arguments to a superclass constructor or invoking a named superclass constructor; otherwise, Dart implicitly calls the superclass’s default, no-argument constructor. Initializer lists are also used to enforce assert statements during development.
class Vector {
  final double x;
  final double y;
  final double squaredMagnitude;

  // Initializer list evaluating an expression before instantiation completes
  Vector(this.x, this.y) : squaredMagnitude = (x * x + y * y);
}

Redirecting Constructors

A constructor can delegate initialization to another constructor within the same class. This is done using a colon followed by this(...). A redirecting constructor cannot have a body.
class Point {
  double x;
  double y;

  Point(this.x, this.y);

  // Redirects to the main generative constructor
  Point.alongXAxis(double x) : this(x, 0);
}

Constant Constructors

If an object’s state must remain immutable and can be determined at compile time, Dart uses constant constructors. The class must declare all instance variables as final, and the constructor is prefixed with the const keyword. Invoking it with const yields a compile-time constant, canonicalized instance.
class ImmutablePoint {
  final double x;
  final double y;

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

Factory Constructors

A factory constructor does not implicitly allocate a new instance of its class. Instead, it acts as a static method that must explicitly return an instance of the class or a subtype. Because it does not generate an instance directly, a factory constructor does not have access to the this reference.
class Logger {
  final String name;
  static final Map<String, Logger> _cache = <String, Logger>{};

  // Factory constructor returning a cached instance
  factory Logger(String name) {
    return _cache.putIfAbsent(name, () => Logger._internal(name));
  }

  // Private generative constructor
  Logger._internal(this.name);
}

Super Parameters

Introduced in Dart 2.17, super parameters provide a concise syntax to pass arguments directly to the superclass constructor without explicitly invoking super(...) in the initializer list.
class Point {
  final double x;
  final double y;

  Point(this.x, this.y);
}

class Point3D extends Point {
  final double z;

  // super.x and super.y are forwarded to the Point constructor
  Point3D(super.x, super.y, this.z);
}
Master Dart with Deep Grasping Methodology!Learn More