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 assert initializer is a debugging construct in Dart placed within a constructor’s initializer list to validate parameters or internal state before the object is instantiated. It evaluates a boolean expression and throws an AssertionError if the condition evaluates to false. Assert initializers are strictly development-time checks; the Dart compiler completely removes them in production/release builds.

Syntax and Placement

Assert initializers are declared after the constructor’s colon (:) and before the constructor body. They are separated from other initializers (such as field assignments or super calls) by commas. The syntax accepts a mandatory boolean condition and an optional string message:
class Rectangle {
  final double width;
  final double height;

  Rectangle(this.width, this.height) 
      : assert(width > 0, 'Width must be strictly positive'),
        assert(height > 0, 'Height must be strictly positive') {
    // Constructor body executes after the initializer list
  }
}

Execution Lifecycle

  1. Argument Binding: Constructor arguments are evaluated and bound to parameters.
  2. Initializer List Execution: The initializer list executes from left to right. Assertions are evaluated in the order they appear.
  3. Superclass Constructor: The implicit or explicit superclass constructor is invoked.
  4. Constructor Body: The block body of the constructor executes.
Because assert initializers run during step 2, the object is not yet fully constructed. Therefore, you cannot reference this or access instance methods within the assert condition. You may only reference the constructor’s parameters, static members, or top-level variables.

Constant Constructors

Assert initializers are fully compatible with const constructors. When a const constructor is invoked to create a compile-time constant, the assert initializer is evaluated by the compiler. If the assertion fails during compile-time evaluation, it halts compilation and produces a compile-time error rather than a runtime AssertionError.
class Fraction {
  final int numerator;
  final int denominator;

  const Fraction(this.numerator, this.denominator)
      : assert(denominator != 0, 'Denominator cannot be zero');
}

// This triggers a compile-time error:
const invalidFraction = Fraction(1, 0); 

// This triggers a runtime AssertionError (in debug mode):
var dynamicFraction = Fraction(1, 0); 

Redirection Constructors

When using a redirecting constructor, assert initializers can be placed before the redirection call. However, they cannot be combined with field initializations or super calls in this context.
class Temperature {
  final double celsius;

  Temperature(this.celsius);

  Temperature.fromKelvin(double kelvin) 
      : assert(kelvin >= 0, 'Absolute zero violation'),
        this(kelvin - 273.15);
}
Master Dart with Deep Grasping Methodology!Learn More