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.
assert(condition);
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.
To enable assert, the --enable-asserts
should be used when running the Dart program in the command line.
The following syntax is used to enable assert in Dart:
dart --enable-asserts my_file_name.dart
Note: The
my_file_name.dart
is the name of the file.
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");}
Lines 1–3: We define a function called addNumber()
, which takes two parameters, x
and y
, 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 variable result
.
Line 9: We use the assert
keyword to check if the runtime type of the result is an integer (int).
Note: If it returns
true
, the program continues running. Otherwise, anassertion error
is thrown.
result
.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");}
Lines 1–17: We define a function called startWithJ()
which takes a list employees
as a parameter, and returns the a list containing names starting with the letter J
.
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 employees
is not empty.
Line 10-15: We use a for...in
loop to iterate over the list. Next, we used the if
statement to filter names starting with J
. Finally, we add the names to the newList
.
Line 16: We return the newList
to the function startWithJ()
using the return
keyword.
Line 19: We define the main()
function.
Line 20: We define a new variable called names
and assign a list of items to it.
Line 21: We call the startWithJ()
function with the name
list passed as a parameter and assign it to a new variable called myList
.
Note:
myList
will hold the value returned from the functionstartWithJ()
.
myList
.