Assert Statements in Dart

As a programmer, it is very necessary to make an errorless code is very necessary and to find the error is very difficult in a big program. Dart provides the programmer with assert statements to check for the error. The assert statement is a useful tool to debug the code and it uses boolean condition for testing. If the boolean expression in assert statement is true then the code continues to execute, but if it returns false then the code ends with Assertion Error.

Syntax: assert(condition);

It must be noted that if you want to use assert then you have to enable it while execution as it can only be used in the development mode and not in productive mode. If it is not enabled then it will be simply be ignored while execution.

Enable the assert while executing a dart file via cmd as:

dart --enable-asserts file_name.dart

Example 1: Using assert in a dart program.

 



Dart

filter_none

edit
close

play_arrow

link
brightness_4
code

void main()
{
  String geek = "Geeks For Geeks";
  assert(geek != "Geeks For Geeks");
  print("You Can See This Line Geek as a Output");
}

chevron_right


 

Output when Asserts are enabled:

Unhandled exception:
'file:///C:/Users/msaur/Desktop/GeeksForGeeks.dart': Failed assertion: line 4 pos 10: 'geek != "Geeks For Geeks"': is not true.
#0      _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:42:39)
#1      _AssertionError._throwNew (dart:core-patch/errors_patch.dart:38:5)
#2      main (file:///C:/Users/msaur/Desktop/GeeksForGeeks.dart:4:10)
#3      _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:301:19)
#4      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)

Output through cmd:

Asserts output through cmd in Dart

Output when Asserts are not enabled:

You Can See This Line Geek as a Output

Output through cmd:

Asserts(not enabled) output through cmd in Dart



Apart from that, you can also send a message for the case if the assert returns false as:

assert(condition, "message");

It is very useful when you are trying to debug various errors and want to know which assert returned the error in the code.

Example 2: Using assert to give the message in a dart program.

Dart

filter_none

edit
close

play_arrow

link
brightness_4
code

void main()
{
  String geek = "Geeks For Geeks";
  assert(geek != "Geeks For Geeks", "Strings are equal So this message is been displayed!!");
  print("You Can See This Line Geek as a Output if assert returns true");
}

chevron_right


 

Output:

C:\Users\msaur\Desktop>dart --enable-asserts GeeksForGeeks.dart
Unhandled exception:
'file:///C:/Users/msaur/Desktop/GeeksForGeeks.dart': Failed assertion: line 4 pos 10: 'geek != 
"Geeks For Geeks"': Strings are equal So this message is been displayed!!
#0      _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:42:39)
#1      _AssertionError._throwNew (dart:core-patch/errors_patch.dart:38:5)
#2      main (file:///C:/Users/msaur/Desktop/GeeksForGeeks.dart:4:10)
#3      _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:301:19)
#4      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)

Output through cmd:

Using assert to give the message in a dart program




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.