Dart Tutorial

Dart Tutorial Single-Page Application Architecture Dart Features Dart Installation Guide Dart Basic Program Dart Syntax Dart Keywords Dart Variables Dart Comments Dart Standard Input Output Dart Important Concepts

Data Types

Built-in Data Types Numbers Strings Booleans Lists Sets Maps Runes and Graphemes Symbols Enumerations Constants Queues

Other data types

Objects Future and stream Iterable Miscellaneous types

OPERATORS

Precedence and associativity Arithmetic operators Equality and Relational operators Type Test Operators Assignment Operators Logical Operators Bitwise and Shift Operators Miscellaneous operators

Control Flow Statements

Introduction If statement If-else statement If-else-if statement Loops Switch and case Dart Break And Continue Assert In Dart

FUNCTIONS

Dart function Types of Functions Anonymous function main( ) function Lexical scope and closure Recursion Common Collection Methods

Object Oriented Concepts

Dart Object-Oriented Concepts Dart Classes Dart Constructors Dart This Keyword Dart Super Keyword Static Members Method Overriding Dart Interfaces Inheritance Dart Abstract Classes Dart Builder Classes Dart Callable Classes

Dart Type System

Dart Type System Dart Soundness Dart Type Inference

MISCELLANEOUS

Dart Isolates Dart Typedef Dart Metadata Dart Packages Dart Generics Dart Generators Dart Concurrency Dart Unit Testing Dart Html Dom Dart URIs Dart Extends, With and Implements Keywords Dart Optional Parameters Rust Vs Dart C++ vs Dart Golang Vs Dart Dart Basics Exception Handling

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 )