Assert Statements in Dart - GeeksforGeeks

20 Jul, 2020
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
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:
Output when Asserts are not enabled:
You Can See This Line Geek as a Output

Output through cmd:
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:
Dart-Exception-Handling
Dart
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Flutter - DropDownButton Widget
Flutter - Custom Bottom Navigation Bar
Flutter - Checkbox Widget
Flutter - Row and Column Widgets
Dart Tutorial
Flutter - BoxShadow Widget
Flutter - Flexible Widget
ListView Class in Flutter
Container class in Flutter
Flutter - Stack Widget