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.dart is 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 value
var result = addNumber(9, 13);
// checks the runtimeType of result
assert(result.runtimeType == int );
// displays the value
print("Result: $result");
}

Code explanation

  • 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, an assertion error is 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 1
assert(employees.length > 1);
// Create a new list
var newList = [];
// Checks the first item in the list
assert(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 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 function startWithJ().

  • Line 22: Finally, we display the result for the variable myList.
Copyright ©2024 Educative, Inc. All rights reserved