Open In App

Assert Statements in Dart

Improve
Improve
Like Article
Like
Save
Share
Report

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 production 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




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


 

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




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");
}


 

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



Last Updated : 19 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads