thumbnail

Assert statement in Dart

Published on 29 March 2020
Last Updated on 29 March 2020

Assert statement is one of the control flow statements in Dart.

Use of Assert statement

Assert statements are mostly used during development to check whether a statement is factually correct are not.

Assert statements are widely used in test files to check whether a statement is factually correct or not.

If a statement is not factually correct as per conditions specified in assert statement then program flow is disrupted.

Program using Assert statement

Given below is a Dart program that demonstrates use of Assert statement in Dart.

Successful Assert Statements

Program give below demonstrates a set of assert statements that are factually correct.

// basic assert example
main(List<String> args) {
  var number1 = 1;
  assert(number1 != null, 'number1 can not be null');
  assert(number1 == 1, 'number1 should be 1');
  assert(number1.toString() == '1', 'number1.toString() should be \'1\'.');
  assert(number1 + 1 == 2, 'number1 +1  should be 2.');
  print('all assertion checks have passed');
}

Above program produces following output:

all assertion checks have passed

Because, above statements are factually correct, program flow is not interrupted and program successfully exits.

Example of Assert statement with a failed assertion

If for example, an assertion fails to evaluate properly then an exception is thrown, given below is an example that demonstrates how this works.

Program given below intentionally makes use of a faulty add function that performs subtraction instead of addition and outputs an incorrect result.

// assert example with a failed assertion

// faulty function
// performs subtraction instead of addition
int add(int a, int b) {
  return a - b;
}

main(List<String> args) {
  assert(add(2, 1) == 3, 'addition of 2 and 1 must be 3');
}

Above program throws following exception:

Unhandled exception:
'asert.dart': Failed assertion: line 8 pos 10: 'add(2, 1) == 3': addition of 2 and 1 must be 3
...
...
...

As shown above, assert statement is very useful for checking if specif parts of your code are factually correct and whether they are producing intended results.

Example of Assert statement with no error message

Second argument passed to assert statement is the error message, however this message can be omitted.

// assert example with omitted error message

// faulty function
// performs subtraction instead of addition
int add(int a, int b) {
  return a - b;
}

main(List<String> args) {
  assert(add(2, 1) == 3);
}

Above program throws following exception:

Unhandled exception:
'assert.dart': Failed assertion: line 10 pos 10: 'add(2, 1) == 3': is not true.
...
...
...

As you can see, from above example if no error message is passed to assert statement, then error message for the exception is automatically generated.

How assertions work in development and production mode

Development only tools such as dartdevc keep assertions enabled. However, in production mode. Assert statements are ignored.

We hope that you have found this guide useful for learning assert statements in Dart.