×

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 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 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 Maps

Map is an object-based data structure that associates keys and values that can be of any data type. Every key can occur only once, while the values can be used multiple...

5 minutes read.

Type System in Dart

Dart language is a type safety enabled language that uses static and dynamic type checking to match the value of the variable with its data type at the compile-time. This...

4 minutes read.

Abstract Classes in Dart

Abstract classes in Dart are those classes that only contain abstract methods  (methods that do not contain any implementation). These classes are specifically designed for the purpose of inheritance. We...

4 minutes read.

Dart Queues

A queue is a user-defined collection of data. It is based on the FIFO principle ( First In First Out ) which implies that the element to be inserted first,...

3 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 super keyword

The ' super ' keyword is used to refer to the immediate parent class object of the currently in-use child class. Using this keyword, we can invoke the superclass (...

6 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 Concurrency

Dart concurrency allows us to run multiple or multiple components of a system at once. It issues several commands at once. Dart provides Isolates as a tool for performing compliance...

2 minutes read.

Dart Type inference

The task of interpreting the data types of fields, methods, local variables and most generic type arguments is handled by the static analyser. However, when not enough information is provided...

5 minutes read.

Lexical Scope and Closure

Lexical Scope : Lexical scope is a very important term in any programming language. It defines the scope of the variable based on the block it is contained inside. According to...

2 minutes read.

Dart Important Concepts

1. Anything placed in a variable is an object, and an object is an instance of a class. Numbers, functions, and null are all objects in Dart, and all these...

3 minutes read.

Dart Loops

Looping refers to repeating or reusing the same lines of code repeatedly until a particular test condition evaluates to true. It is also known as iterating.  It is effectively used when...

4 minutes read.

Dart Runes and Graphemes

Runes and Graphemes data types in Dart Runes and Graphemes Runes are the special string of Unicode UTF-32 bits that represent the special syntax. Unicode defines a unique numeric value for each...

2 minutes read.

Builder Classes in Dart

Before understanding builder classes, let us understand about Flutter that makes the best use of the builder classes. Flutter is actually not a programming language, it is a software development...

6 minutes read.

Callable Classes in Dart

Just like the functions, we can also call the instances of classes in Dart. Such classes are also known as “callable ” classes. We need to use the “call( )”...

3 minutes read.

Dart Comments

Comments are the statements that are used to enhance the readability and understandability of the code. The compiler does not execute these lines. It simply ignores these comments when it scans...

2 minutes read.

Dart Object-Oriented Concepts

Dart is a programming language that supports object-oriented programming. It is focused on the objects that are real-world entities and supports all the concepts of OOPS, such as objects, classes,...

4 minutes read.

Dart this Keyword

The ' this ' keyword, similar to that in Java and C#, refers to an object of the class using it. It points to the current object of the class,...

5 minutes read.