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

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

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); 
Master Dart with Deep Grasping Methodology!Learn More