Dart Assert Statement

In this tutorial you will learn about the Dart Assert Statement and its application with practical example.

Dart Assert Statement

The assert statement is a useful debugging tool, is used for testing boolean conditions. An assert statement disrupt normal execution if a boolean condition is false. If the boolean expression is true, then the code continues to execute normally. If assert statement results in false, then execution ends with an AssertionError.

Syntax:-

Example:-

Note :- Assert statements have no effect in production mode; it is used in development mode only.

Enable Assert

if you’re executing a dart file via command line, you need to enable asserts as follows –

Assert with Message

To attach a message to an assert statement, pass a string as the second argument.

Syntax:-

The first argument is an expression that evaluates to a boolean value. If the expression’s value is true, the assertion succeeds and execution continues. If it’s false, an exception is thrown with the message provided.

In this tutorial we have learn about the Dart Assert Statement and its application with practical example. I hope you will like this tutorial.