×

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]

Related Topics

Rust vs Dart

Rust is a system-level programming language that stands next to C ++ in terms of syntax, but offers greater speed and memory security. Dart, on the other hand, is an...

2 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.

Dart - extends, with and implements Keywords

The application development in Dart programming language using the Flutter framework, regularly experience different usage of the implements, extends and with keywords. Dart has full support for inheritance that is...

5 minutes read.

Dart Logical Operators

Logical operators are the operators that combine two or more conditions, and accordingly return a Boolean value : true or false. Assume the value of variable A is 25 and B...

2 minutes read.

Dart Type Test Operators

Type Test Operators in Dart These operators are used to check the types of expressions at runtime. Dart is a typed language, and we often want to assert that a value...

1 minute 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.

Dart Enumerations

Enumeration data type in Dart In simple terms, Enumerations are referred to as named constant values. Constant values are declared as enumerations using the ‘enum’ keyword.  Implementation =  enum enum_name {    ...

2 minutes 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.

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 Recursion

Recursion is one of the most important and interesting concepts in any programming language. It can be defined as a process in which a function calls itself directly or indirectly....

5 minutes read.

Dart Equality and Relational Operators

Dart provides the functionality of checking the relationship between the values or values within the variables. These operators return the resultant value as Boolean, i.e., either ‘true’ or ‘false’. Some important points...

3 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.

Metadata in Dart

Metadata is often referred to as the data about the data. It is a  piece of data about a basic piece of data. In the case of a Dart program,...

2 minutes read.

Dart Features

Dart is a fully-featured, object-oriented modern language with its roots in the programming language ‘Smalltalk’. It has some of its eminent features similar to the languages such as Java, C#...

4 minutes 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 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.

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 Iterable

Iterable is a collection of elements that are accessed sequentially. These elements are accessible using the iterator getter and stepping through the values using this getter. Let us understand the setting...

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