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

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 list = [ 4, 2, 8 ];

This is a list of integer type values.

              var list2 = [ ‘India’, ‘USA’, ‘Japan’ ];

This is a list of string type values.

Every element is indexed to a value. Indexing begins from [0] and ends at [length - 1] index.

In the above examples, the size of the list is 3, beginning from the 0th index and ending at the 2nd index.

A newer version of Dart, Dart 2.3 introduced new operators viz, spread operator (...) and the null – aware spread operator (...?) which provides a feature of inserting multiple values into the list.

  1. Spread Operator (...) is used to insert all the values of a list into another list.

Example,

  var L1 = [ 1, 2, 3 ];

  var L2 = [ 0, . . . L1 ];

  1. Null-aware spread operator (...?) is used when the expression on the right side might be null.

Example,

  var L1;

  var L2 = [ 0, . . . ?L1 ];

Example :

Consider the following code in Dart to understand the above point more clearly,

void main( )
{
  // defining a list
  var list = [ 1, 2, 3 ];
  var list1;
  // prints the length of the list
  print( list.length );
  // prints the element at the 1st index of the list
  print( list [ 1 ] );
  //inserts value of the list in list2
  var list2 = [ 0,...list ];
  // inserts value of list1 in list2. ? is used to check in case the list1 is null
  var list3 = [ 0,...?list1 ];
  print( list2 );
  print( list3 );
}

Output :

3
2
[0, 1, 2, 3]
[0]

Types of Lists

The Dart list is categorized as follows :

  1. Fixed length list
  2. Growable list

Fixed length list

As the name suggests, the length of this type of list is fixed, which is predefined, while declaring the list. Since the size is fixed, it cannot be changed at runtime.

Syntax :

var list_name = new List( size );

This creates a list of fixed length to which elements cannot be added or deleted at runtime. If, in any case, modification is made in the list with respect to its size, the compiler throws an exception.

This might not work when null safety is enabled. In that case, use List<E>.filled constructor. This constructor creates a list of the given length with values filled at each position.

Implementation = List< E >.filled( int length, E fill, { bool growable = false } )

Consider the following code that depicts the fixed length list:

void main( )
{
  // declaration of fixed length list
  var fixedList = new List< int >.filled( 5, 0, growable : false );
  // inserting values in the fixed length list
  fixedList[ 0 ] = 1;
  fixedList[ 1 ] = 2;
  fixedList[ 2 ] = 3;
  fixedList[ 3 ] = 4;
  fixedList[ 4 ] = 5;
  // fixedList[ 5 ] = 6; Including this line throws an error
  // printing elements of the fixed length list
  print( ' \n Elements in the list are as follows: $fixedList ' );
}

Output:

Elements in the list are as follows: [1, 2, 3, 4, 5]

Growable list

This type of list doesn’t have a fixed length; therefore, it can be extended to hold any number of elements. The size of the list is not mentioned while declaring it, and hence it can be modified at the run time.

Syntax :

var list_name = new List( );

This creates a list of variable length to which elements can be added or deleted at runtime. Here, modifications can be made to the list with respect to its size.

This might not work when null safety is enabled. In that case, use List<E>.filled constructor. This constructor creates a list of the given length with values filled at each position.

Implementation = List< E >.filled( int length, E fill, { bool growable = false } )

Consider the following code that depicts the fixed length list:

void main( )
{
  // declaring growable list
  var growableList = new List< int >.filled( 0, 0, growable: true );
  // initializing growable list with values
  growableList = [ 0, 1, 2, 3, 4, 5 ];
  print( ' The elements in the growable list are : $growableList ' );
  // adding element 6 to the growable list
  growableList.add( 6 );
  print( ' After adding 6 to the list, he elements in the growable list are : $growableList ' );
}

Output

The elements in the growable list are : [0, 1, 2, 3, 4, 5]
After adding 6 to the list, he elements in the growable list are : [0, 1, 2, 3, 4, 5, 6]

There is another categorization of list bases on their dimensions as follows :

  1. 1 – Dimensional list ( 1 - D) List
  2. 2 – Dimensional list ( 2 - D) List
  3. 3 – Dimensional list ( 3 - D) List

We have already discussed 1 – Dimensional list ( 1 – D ) in the above sections.

2 – Dimensional list ( 2 – D )

The list is defined in two dimensions with respect to rows and columns, just like in a table.

3 – Dimensional list ( 3 – D )

The list is defined in three dimensions with respect to x, y and z, a little similar to 2 – D list.

List Properties :

Property

Description

first

Returns the first element and throws an error if the first element is null. Therefore, the list must be non-empty. 

Implementation = void set first( E value );

isEmpty

Checks if there are any elements in the collection and accordingly returns true or false. 

Implementation = bool get isEmpty -> ! iterator.moveNext( );

isNotEmpty

Checks if there is at least one element in the collection and accordingly returns true or false. 

Implementation = bool get isNotEmpty -> ! isEmpty;

length

Calculates the number of objects in the list and returns the same. It starts counting from the 0th index.  

Implementation = int get length;

last

Returns the last element of the list and throws StateError if the list is empty. 

Implementation = void set last ( E value );

reversed

Returns a list in reverse order. 

Implementation = Iterable< E > get reversed;

Single

Checks if the list has only one element and returns that particular

element. 

Implementation = E get single { return result };

hashCode

All objects have some hash codes, which are integer values representing

the state of the object. 

Implementation = external int get hashCode;


List Methods :

  1. add( )

The add( ) function is used to add a new value to the end of the list. One limitation is that it can add only one element at a time and returns the new list after addition.

Implementation = list_name.add(element);

  1. addAll( )

The addAll( ) function is used to add multiple values to the list separated by a comma and enclosed within a square bracket ([  ]).

Implementation = list_name.addAll([val1, val2, val3, …, valn]);

  1. insert( )

The insert( ) function is used to insert an element at the specific index mentioned within the round braces ‘(  )’.

Implementation = list_name.insert(index, value);

  1. insertAll( )

The insertAll( ) function is used to insert multiple values at a specific index mentioned within the round braces ‘(  )’.

Implementation = list_name.insertAll(index, [list of values separated by comma]);

  1. replaceRange( )

The replaceRange(  ) function is used to update the list of items lying in a specified range.

Implementation = list_name.replaceRange(int start_val, int end_val, iterable);

  1. remove( )

The remove(  ) function is used to remove one element at a time from the list, and that element is passed to this function as an argument.

Implementation = list_name.remove(value);

  1. removeAt( )

The removeAt( ) function is used to remove an element from the specific index and returns that element.

Implementation = list_name.removeAt(int index);

  1. removeLast( )

The removeLast( ) function is used to remove the last element of the list.

Implementation = list_name.removeLast( );

  1. removeRange( )

The removeRange( ) function is used to remove the items within the specific range, which is passed to the function as an argument.

Implementation = list_name.removeRange( int start, int end );

Consider the following code in Dart to understand the above point more clearly,

Source Code

void main( )
{
  var list1 = [ 1, 2, 3 ];
  var list2 = [ 1 ];
  print( ' \nList 1 : ' );
  print( list1 );
  print( ' \nList 2 : ' );
  print( list2 );
  list1.add( 4 );
  print( ' \nList 1 after adding 4 : ' );
  print( list1 );
  list1.addAll( [ 5, 6, 7, 8 ] );
  print( ' \nList 1 after adding 5, 6, 7, 8 : ' );
  print( list1 );
  list2.insert( 1, 11 );
  list2.insert( 2, 12 );
  print( ' \nList 2 after adding 11 and 12 at 1st and 2nd index : ' );
  print( list2 );
  list2.insertAll( 2, [ 13, 14, 15, 16 ] );
  print( ' \nList 2 after adding 13, 14, 15, 16 at index 2 : ' );
  print( list2 );
  list2.replaceRange( 2, 4, [ 21, 22, 23 ] );
  print( ' \nList 2 after replacing elements between 2nd and 4th index  with 21, 22, 23 :' );
  print( list2 );
  list2.remove( 21 );
  print( ' \nList 2 after removing 21 value : ' );
  print( list2 );
  list1.removeAt( 2 );
  print( ' \nList 1 after removing element at the 2nd index : ' );
  print( list1 );
  list2.removeLast( );
  print( ' \nList 2 after removing the last element : ' );
  print( list2 );
  list1.removeRange( 1, 3 );
  print( ' \nList 1 after adding removing the elements between 1st and 3rd index : ' );
  print( list1 );
}

Output:

List 1 :
[1, 2, 3]
List 2 :
[1]
List 1 after adding 4 :
[1, 2, 3, 4]
List 1 after adding 5, 6, 7, 8 :
[1, 2, 3, 4, 5, 6, 7, 8]
List 2 after adding 11 and 12 at 1st and 2nd index :
[1, 11, 12]
List 2 after adding 13, 14, 15, 16 at index 2 :
[1, 11, 13, 14, 15, 16, 12]
List 2 after replacing elements between 2nd and 4th index  with 21, 22, 23 :
[1, 11, 21, 22, 23, 15, 16, 12]
List 2 after removing 21 value :
[1, 11, 22, 23, 15, 16, 12]
List 1 after removing element at the 2nd index :
[1, 2, 4, 5, 6, 7, 8]
List 2 after removing the last element :
[1, 11, 22, 23, 15, 16]
List 1 after adding removing the elements between 1st and 3rd index :
[1, 5, 6, 7, 8]