> ## 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.

# Dart Assert Initializer

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:

```dart theme={"dark"}
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`.

```dart theme={"dark"}
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.

```dart theme={"dark"}
class Temperature {
  final double celsius;

  Temperature(this.celsius);

  Temperature.fromKelvin(double kelvin) 
      : assert(kelvin >= 0, 'Absolute zero violation'),
        this(kelvin - 273.15);
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Dart Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
