×

Assert in DART

In any programming language, resolving error is the most unwanted and tedious task. At times it becomes very difficult to find the error in the large code. Dart offers an efficient way of managing error in your code by using an ' Assert ' statement. It debugs the code using the Boolean condition for testing. If the Boolean expression evaluates to true, the program continues its normal execution, while if it evaluates to false, the code then ends with as assertion error.

Given below is the syntax of using the assert statement :

assert( condition ) ;

An important point to note :

It is important to remember that you need to enable the assert statement using command prompt before actually using it. This has to be done while execution as assert statement can be used only in the development mode and not in the productive mode. If you do not enable this statement, the compile will simply ignore it while execution.

Let us see how to enable the assert statement while executing a dart file using command prompt :

dart --enable-asserts file_name.dart

Consider the following example in Dart that uses the assert statement in the program

Example 1 :

void main( ) 
{
  String str = " This is the tutorial of assert statement ! " ;
  assert( str != " This is the tutorial of assert statement ! " ) ;
  print( " This line can be seen as the output on the console ! " ) ;
}

Output :

Without using command prompt :

This line can be seen as the output on the console !

When Asserts are enabled :

ASSERT IN DART

Output through command prompt :

Unhandled exception:
'file:///D:/Dart/assert.dart': Failed assertion: line 4 pos 11: 'str != " This is the tutorial of assert statement ! "': is not true.
#0 _AssertionError._doThrowNew ( dart : core-patch/errors_patch.dart : 46 : 39 )
#1 _AssertionError._throwNew ( dart : core-patch/errors_patch.dart : 36 : 5 )
#2 main ( file:///D :/Dart/assert.dart : 4 : 11 )
#3 _delayEntrypointInvocation.<anonymous closure> ( dart : isolate-patch/isolate_patch.dart : 283 : 19 )
#4 _RawReceivePortImpl._handleMessage ( dart : isolate-patch/isolate_patch.dart : 184 : 12 )

Assert with message

Apart from just catching an error using the assert statement, we can also display a productive message against that assert statement if it evaluates to false.

Syntax of using this is as follows :

assert( condition, ' message ' ) ;

This turns out to be of great advantage when we are handling many errors using the assert statement. This message functionality lets us know which assert statement has actually returned the error in the code.

Consider another example demonstrating the above concept :

Example 2 :

void main( )
{
  String s1 = " This is an assert statement ! " ;
  assert( s1 == " This is an assert statement ! ", " Cool ! The strings are equal. " ) ;
  print( " You Can See This Line on console as an output if the assert returns true. " ) ;
}

Output :

Without using the command prompt :

You Can See This Line on console as an output if the assert returns true.

When Asserts are enabled :

ASSERT IN DART

Output through command prompt :

Unhandled exception:
'file:///D:/Dart/assert.dart': Failed assertion: line 4 pos 10: 's1 == "This is an assert statement!"':  Cool ! The strings are equal.
#0      _AssertionError._doThrowNew ( dart:core-patch/errors_patch.dart : 46 : 39 )
#1      _AssertionError._throwNew ( dart : core-patch/errors_patch.dart : 36 : 5 )
#2      main ( file:///D:/Dart/assert.dart : 4 : 10 )
#3     _delayEntrypointInvocation.<anonymous closure>  ( dart : isolatepatch/ isolate_patch.dart : 283 : 19 )  
#4      _RawReceivePortImpl._handleMessage ( dart : isolate-patch/ isolate_patch.dart : 184 : 12 )

Related Topics

Dart Common Collection Methods

Most of the programming languages have arrays as a way to list items together. However, Dart has a collection of data structures similar to array. These are supported by the...

3 minutes read.

Dart lists data type

The most important data type in any programming language is an ordered list of elements, an array. Dart lists resemble JavaScript array lists. Example,               var...

7 minutes read.

Interfaces in Dart

An interface in Dart refers to the syntax or blueprint that any class must adhere to. It basically defines the array of methods available on the object. It provides the...

3 minutes read.

Dart Standard Input & Output

Standard Input in Dart (stdin): The standard input stream reads data both synchronously and asynchronously from the keyboard.  In Dart programming language, .readLineSync( ) function is used to accept input from the...

3 minutes read.

Dart Miscellaneous Operators

Apart from the operators we studied so far, Dart provides some more operators which are as follows : Conditional OperatorCascade Notations Conditional Operators These are the operators that help in evaluating the expression...

1 minute read.

Golang vs Dart

Go is the procedural programming language. It was founded in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson at Google but was launched in 2009 as the language of...

2 minutes read.

Dart Numbers

Dart provides an in-built data type ‘Numbers’ that provides two types of values: intdouble 1. int data type int data type supports integer values, and their size or values range is platform-dependent. Integers...

4 minutes read.

Dart Generators

Synchronous Generator Dart Generator is a unique function that allows us to generate price sequences. Generators return values when needed; means that the value is generated if we try to duplicate...

5 minutes read.

Dart If statement

If statement is used to set the control on the lines of code. Using if statement, block of code is executed only if the expression in the statement returns true....

1 minute read.

Dart Method Overriding

Before understanding method overriding, it is important to clear the concept of polymorphism. Polymorphism is derived from two Greek words 'Poly' which means many and 'morphs' which means many forms....

5 minutes read.

Dart Construtors

Constructors are the very important concept in any programming language. They are the special functions created in the classes to allocate memory to the objects of that class when created...

4 minutes read.

Dart URIs

The Uri class supports encoding and decoding of strings with the help of functions to be used in URIs ( which may also be known as URLs ). These functions...

6 minutes read.

Dart Static Members

Static Members in Dart Static members are the members of the class declared using the 'static' keyword. the static members have the following characteristics :  All the objects of the class having...

3 minutes read.

Assert in DART

In any programming language, resolving error is the most unwanted and tedious task. At times it becomes very difficult to find the error in the large code. Dart offers an...

3 minutes read.

Dart Switch Case Statement

In the case of if-else statements, we prefer not to use the if-else ladder when there are many test conditions to be evaluated. In such a situation, it is preferable...

4 minutes read.

Dart Booleans

Booleans Dart has a data type named ‘bool’ to represent Boolean values : true and false, which are compile-time constants. ‘True’ or ‘False’ cannot be assigned with values 1 or 0. Example, bool...

1 minute read.

Dart Objects

Apart from the built-in data types, Dart offers some other data types that have a special role to play. One of them is Objects.  Objects Objects are the foundation of the...

3 minutes read.

Dart Generics

Dart Generics is similar to Dart collections, which are used to store homogeneous data. As we have discussed in Dart's features, it is a language with types being optional. By default,...

4 minutes read.

Dart Break and Continue

Loop statements are used to change the normal sequence of flow of the program. Dart supports two types of loop control statements: Break StatementContinue Statement Break Statement : The ‘ break ’ statement is...

4 minutes read.

Unit Testing in Dart

Typing testing ensures that your app keeps working as you add more features or change existing functionality. Unit testing helps to confirm the behavior of a single function, method, or...

4 minutes read.