Assert -Dart

During development, use an assert statement— assert(<condition>, <optionalMessage>); —to disrupt normal execution if a boolean condition is false.

// Make sure the variable has a non-null value.

assert(text != null);

// Make sure the value is less than 100.

assert(number < 100);

// Make sure this is an https URL.

assert(urlString.startsWith(‘https’));

content_copy

To attach a message to an assertion, add a string as the second argument to assert (optionally with a trailing comma):

assert(urlString.startsWith(‘https’),

  ‘URL ($urlString) should start with “https”.’);

The first argument to assert can be any expression that resolves to a boolean value. If the expression’s value is true, the assertion succeeds and execution continues. If it’s false, the assertion fails and an exception (an AssertionError) is thrown.

Leave a comment

Your email address will not be published. Required fields are marked *