Dart Lists

In this tutorial you will learn about the Dart Lists and its application with practical example.

Dart Lists

In Dart, list data type is used to represent a collection of objects. A List is an ordered group of objects. The List data type is actually similar to the concept of an array in other programming languages. An array is used to hold multiple values in single variable, similarly in Dart, an array is a List objects that hold multiple objects/values in single variable , so most people just call them lists. A list variable is defined by having values separated by commas and enclosed within square brackets ([]).

Example:-

Below is logical representation of the list –

logical_representation_of_a_list_1

Here,

list1 :- It is the identifier that used to refer the corresponding list object.

Index:- Individual elements in a list can be accessed easily by index or subscript like list_name[index]. Indexing of a list starts from zero (0) to the last element of list which is list_name[size-1] where size is the number of elements in the list.

Elements:- List elements refers to the actual values/objects stored in a given list.

In Dart, lists can be classified as –

  • Fixed Length List
  • Growable List

Fixed Length List

A List object declared with size specified cannot be changed runtime.

Syntax:-

Below Is Syntax for Declaring Fixed Size List

Syntax:-

Below Is Syntax for Initializing Fixed Size List Elements.

Example:-

Output:-

When you run the above Dart Program, you will see following output –

Growable List

A List object declared without size is termed as Growable List. The length of the growable list can changed in runtime.

Syntax:-

Below Is Syntax for Declaring Growable List

Or

Syntax:-

Below Is Syntax for Initializing Growable List Elements.

Example:-

Here, we have created a list of even numbers and initialized it with four elements and later added one more element to it.

Recommended:-  Dart Generics

Output:-

When you run the above Dart Program, you will see following output –

List Properties

Below is a list of properties supported by Dart List.

Property Description
first It returns the first element case.
isEmpty It returns true if the collection has no elements.
isNotEmpty It returns true if the collection has at least one element.
length It returns length/size of the list, can also be seen as number of elements in a given list.
last It returns the last element in the list.
reversed It returns an iterable object containing the lists values in the reverse order.
Single It is used to checks if the list has only one element and returns it.

Inserting Elements into List

add() :- The add() function is used to append a specified value to the end of the list and returns a modified list object.

Syntax:-

Example:-

Output:-

addAll() :- The addAll() function is used to append multiple values to the given list object. It accepts multiple comma separated values enclosed within square brackets ([]) and appends it to the list.

Syntax:-

Example:-

Output:-

insert() :- The insert() function is used to insert an element at specified position. It accepts a value and inserts it at the specified index.

Syntax:-

Example:-

Output:-

insertAll():- The insertAll() function is used to inserts a list of multiple values to a given list at specified position. It accepts index position and a list of multiple comma separated values enclosed within square brackets ([]) and insert the list values beginning from the index specified.

Recommended:-  Dart Getters and Setters

Syntax:-

Example:-

Output:-

Updating List

The simplest way a list element can be modified by accessing element and assigning it new value.

Syntax:-

Example:-

Output:-

replaceRange() :- The replaceRange() function is used to update a range of list items. This function updates the value of the elements within the specified range.

Syntax:-

Example:-

Output:-

Removing List Elements

remove() :- The remove() function is used to remove an elements from the list. It removes the first occurrence of a specified element in the list. It returns true if the specified element is removed from the list.

Syntax:-

Example:-

Output:-

removeAt() :- The removeAt() function is used to remove an element from specified index position and returns it.

Recommended:-  Dart Bitwise operators

Syntax:-

Example:-

Output:-

removeLast() :- The removeLast() function is used to remove and returns the last element from the given list.

Syntax:-

Example:-

Output:-

removeRange() :- The removeRange() function is used to remove the items within the specified range. It accepts the start and end index position and removes all elements between the specified range.

Syntax:-

Example:-

Output:-

Dart Iterating List Elements

In Dart, we can loop through the list elements using forEach method as following –

Example:-

Output:-

dart_iterating_list_elements

In this tutorial we have learn about the Dart Lists and its application with practical example. I hope you will like this tutorial.