What is assert in Dart?
Creating error-free code is crucial for programmers, and it can be quite challenging to identify errors in large programs. Dart provides an assert statement that helps programmers debug their codes.
The assert statements in Dart allow the programmer to look for errors during code development. It is also used in unit testing.
Syntax
assert(condition);
How does it work?
To test the code, the assert statement uses a boolean condition, which is a helpful tool for debugging. The assert statement’s boolean expression must return true for the code to continue running. If it does not, the code terminates with an assertion error.
The example of a boolean expression used in an assert statement is:
String x = 'Book';
assert(result != null );
In the boolean expression, we check that the value of x is not null using the != (not null) operator.
If the value of x is null, a false return and an assertion error is thrown. Otherwise, the code continues running.
Note: To use an assert statement, we have to enable it. The statement only works in debug mode and not in production mode.
How to enable assert statement in Dart
To enable assert, the --enable-asserts should be used when running the Dart program in the command line.
Syntax
The following syntax is used to enable assert in Dart:
dart --enable-asserts my_file_name.dart
Note: The
my_file_name.dartis the name of the file.
Code example 1
The following code shows how to use assert in Dart:
int addNumber(int x, int y){return x + y;}void main() {// assigns the valuevar result = addNumber(9, 13);// checks the runtimeType of resultassert(result.runtimeType == int );// displays the valueprint("Result: $result");}
Code explanation
-
Lines 1–3: We define a function called
addNumber(), which takes two parameters,xandy, and returns the addition of both parameters. -
Line 5: We define the
main()function. -
Line 7: We call the
addNumber()function and assign its value to a new variableresult. -
Line 9: We use the
assertkeyword to check if the runtime type of the result is an integer (int).
Note: If it returns
true, the program continues running. Otherwise, anassertion erroris thrown.
- Line 11: Finally, we display the value for the variable
result.
Code example 2
Let’s look at another example:
startWithJ(List employees) {// checks that the length of the list passed// is greater than 1assert(employees.length > 1);// Create a new listvar newList = [];// Checks the first item in the listassert(employees[0] != '');//returns a new list containing the employees//whose names start with the letter 'J'for (final element in employees) {if(element.startsWith('J')) {newList.add(element);}}return newList;}void main() {var names = ['John', 'Alvaro', 'Joe', "Joyce"];var myList= startWithJ(names);print("Names Starting with J: $myList");}
Code explanation
-
Lines 1–17: We define a function called
startWithJ()which takes a listemployeesas a parameter, and returns the a list containing names starting with the letterJ. -
Line 4: We use the assert statement to check the length of the List passed to the function
startWithJ(). -
Line 6: We created a new empty List called
newList. -
Line 8:We use the assert statement to check that the first item in the List
employeesis not empty. -
Line 10-15: We use a
for...inloop to iterate over the list. Next, we used theifstatement to filter names starting withJ. Finally, we add the names to thenewList. -
Line 16: We return the
newListto the functionstartWithJ()using thereturnkeyword. -
Line 19: We define the
main()function. -
Line 20: We define a new variable called
namesand assign a list of items to it. -
Line 21: We call the
startWithJ()function with thenamelist passed as a parameter and assign it to a new variable calledmyList.
Note:
myListwill hold the value returned from the functionstartWithJ().
- Line 22: Finally, we display the result for the variable
myList.
Free Resources