Sitemap
Dart Tutorials

Dart Tutorials for beginners and advanced programmers

Assert in Dart

3 min readMar 25, 2025

--

The assert statement in Dart is used to verify conditions that should always be true during development. If the condition fails (evaluates to false), Dart throws an AssertionError. This helps catch bugs early before they cause serious issues.

Press enter or click to view image in full size

Read Medium Story for free 👇

Dart Assert Syntax:

assert(condition, [optionalMessage]);
  • condition: A boolean expression that should evaluate to true.
  • • optionalMessage (optional): A message displayed if the assertion fails.

Important Notes:

  • Assertions only work in debug mode. In release mode, they are ignored.
  • They are useful for debugging but should not be relied upon for error handling in production.

Examples of assert in Dart

1. Basic Assertion

void main() {
int age = 25;
assert(age > 0, 'Age must be greater than zero');
print('Assertion passed!');
}

--

--

Usama Sarwar
Usama Sarwar

Written by Usama Sarwar

Helping founders turn ideas into market-ready web and mobile apps within 30 days using Google’s Flutter & Next.js without juggling multiple teams.

Dart Tutorials
Dart Tutorials

Published in Dart Tutorials

Dart Tutorials for beginners and advanced programmers