Skip to main content

Command Palette

Search for a command to run...

Dart : Assert statement | Assertions

Published
1 min read
Dart : Assert statement | Assertions
J

i am Jeet Bhalu i am flutter App developer

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.

More from this blog

J

Jeet Bhalu

191 posts