Dart: Assert statement | Assertions

Dart: Assert statement | Assertions

ยท

1 min read

The assert statement in Dart is a way to test a boolean condition at runtime and throw an exception if the condition is false. The assert statement is used to ensure that certain conditions are met and to prevent errors from occurring in the app.

Assert is one type of debugging tool in dart. We can use this tool when our project are big and undeveloped.

It show self generated error message.

By default in our code "assert" is not enable , so for enabling it we have to write one command in our terminal which is " dart --enable-assert file_name.dart "

Syntax:

void main()
{

  assert(condition);

}
void main()
{
  int checkAssertFunction = 20;
  assert(checkAssertFunction ==10);
  print("Value is right");
}

When our assert condition is true at that time we receive our print statement.

When our assert condition is not true at that time we receive below output in our debug consul .

ย