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

An `assert` statement is a debugging and validation construct that evaluates a boolean expression, immediately halting execution or compilation if the expression evaluates to `false`. Depending on the context, a failed assertion either throws an `AssertionError` at runtime or triggers a compile-time error.

## Syntax

```dart theme={"dark"}
assert(condition, [message]);
```

## Parameters

* **`condition`**: A required expression that must evaluate to a `bool`. If the condition evaluates to `true`, execution proceeds normally. If `false`, the assertion fails and throws an `AssertionError`. In Dart, an `AssertionError` is a subclass of `Error` (representing a programming mistake that should not be caught), strictly distinct from an `Exception`.
* **`message`**: An optional second argument of type `Object?`. If provided, it is evaluated and passed directly to the `AssertionError` constructor, where it is stored in the `message` property to provide specific context regarding the failure.

## Constructor Initializer Lists

Dart permits the use of `assert` statements directly within a constructor's initializer list. This syntactic feature allows for the validation of constructor parameters before the constructor body executes or the object is fully instantiated.

```dart theme={"dark"}
class Point {
  final int x;
  final int y;

  Point(this.x, this.y) : assert(x >= 0, 'x must be a non-negative integer');
}
```

## Compilation and Evaluation Behavior

The Dart compiler handles `assert` statements differently depending on the build environment and the instantiation context:

* **Debug Mode**: Assertions are actively evaluated at runtime. In the standalone Dart VM, they are enabled using the `--enable-asserts` command-line flag. Frameworks like Flutter enable them by default during standard debug builds.
* **Release Mode**: The compiler omits `assert` statements from the final generated executable code, ensuring they incur zero CPU or memory overhead in production binaries. However, the compiler still parses and type-checks the expressions inside `assert` statements. A syntax or type error within an `assert` will still break a release build.
* **Compile-Time Constant Evaluation**: When an `assert` is used in a `const` constructor to create a compile-time constant, it is evaluated at compile time. If the condition evaluates to `false` during const evaluation, it halts compilation and produces a compile-time error.

## Code Examples

```dart theme={"dark"}
void main() {
  int port = 8080;

  // Standard runtime assertion
  assert(port >= 1024);

  // Assertion with an optional message
  assert(port <= 65535, 'Port number $port is out of the valid range.');
}

// Compile-time assertion in a const constructor
class Config {
  final int timeout;
  const Config(this.timeout) : assert(timeout > 0, 'Timeout must be positive');
}

// Evaluates to false at compile time, halting compilation with an error:
// const Config invalidConfig = Config(-1); 
```

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