Assert Statements in Dart
Last Updated :
14 Apr, 2025
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 a Boolean condition for testing. If the Boolean expression in the assert statement is true, then the code continues to execute, but if it returns false, then the code ends with an 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
Using assert
Example:
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.
Using assert to give the message
Example:
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:
Real World Example:
Usually, when there is no money in our account, it shows an error such as: "Transaction not possible because you have a zero balance in your account!" We can achieve this using asserts in the above program.
Dart
void main() {
double accountBalance = 0; // Account balance in rupees
// Assertion to ensure sufficient balance before proceeding
assert(accountBalance > 0, "Transaction not possible because you have a zero balance in your account!");
print("Transaction successful. You have sufficient funds.");
}
Output:
Uncaught Error, error: Error: Assertion failed: file:///tmp/dartpadHATVQD/lib/main.dart:5:5
accountBalance > 0
"Transaction not possible because you have a zero balance in your account!"
Explore
Dart Tutorial
7 min read
Basics
Data Types
Control Flow
Key Functions
Object-Oriented Programming
Dart Utilities
Dart Programs
Advance Concepts