Dart : Assert statement | Assertions

Jeet BhaluJeet Bhalu
1 min read

Assert Statement :

  • 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'));
    
  • 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.

0
Subscribe to my newsletter

Read articles from Jeet Bhalu directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Jeet Bhalu
Jeet Bhalu

i am Jeet Bhalu i am flutter App developer