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:-
1 |
var list1 = [11, 12, 13]; |
Below is logical representation of the list –
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
1 |
var list_name = new List(size) |
Syntax:-
Below Is Syntax for Initializing Fixed Size List Elements.
1 |
list_name[index] = value; |
Example:-
1 2 3 4 5 6 7 |
void main() { var lst = new List(3); lst[0] = 11; lst[1] = 12; lst[2] = 13; print(lst); } |
Output:-
When you run the above Dart Program, you will see following output –
1 |
[11, 12, 13] |
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
1 2 |
//creates a list with values var list_name = [val1,val2,val3] |
Or
1 2 |
//creates a list of size zero var list_name = new List() |
Syntax:-
Below Is Syntax for Initializing Growable List Elements.
1 |
list_name[index] = value; |
Example:-
1 2 3 4 5 6 |
void main() { var even_list = [2,4,6,8]; print(even_list); even_list.add(10); print(even_list); } |
Here, we have created a list of even numbers and initialized it with four elements and later added one more element to it.
Output:-
When you run the above Dart Program, you will see following output –
1 2 |
[2, 4, 6, 8] [2, 4, 6, 8, 10] |
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:-
1 |
<list_name>.add(<value>); |
Example:-
1 2 3 4 5 6 |
void main() { var even_list = [2,4,6,8]; print(even_list); even_list.add(10); print(even_list); } |
Output:-
1 2 |
[2, 4, 6, 8] [2, 4, 6, 8, 10] |
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:-
1 |
<list_name>.addAll([val1, val2, val3....valN]); |
Example:-
1 2 3 4 5 6 |
void main() { var even_list = [2,4]; print(even_list); even_list.addAll([6,8,10]); print(even_list); } |
Output:-
1 2 |
[2, 4] [2, 4, 6, 8, 10] |
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:-
1 |
list_name.insert(index,value) |
Example:-
1 2 3 4 5 6 |
void main() { List l = [2,3,4]; print(l); l.insert(0,1); print(l); } |
Output:-
1 2 |
[2, 3, 4] [1, 2, 3, 4] |
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.
Syntax:-
1 |
list_name.insertAll(index, iterable_list_of_values) |
Example:-
1 2 3 4 5 6 |
void main() { var even_list = [2,10]; print(even_list); even_list.insertAll(1,[4,6,8]); print(even_list); } |
Output:-
1 2 |
[2, 10] [2, 4, 6, 8, 10] |
Updating List
The simplest way a list element can be modified by accessing element and assigning it new value.
Syntax:-
1 |
list_name[index] = new_value; |
Example:-
1 2 3 4 5 6 |
void main() { var even_list = [2,4,5,8,10]; print("List values before update element : ${even_list}"); even_list[2] = 6; print("List values after updating list element : ${even_list}"); } |
Output:-
1 2 |
List values before update element : [2, 4, 5, 8, 10] List values after updating list element : [2, 4, 6, 8, 10] |
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:-
1 |
list_name.replaceRange(int start_val,int end_val,iterable_list_of_values) |
Example:-
1 2 3 4 5 6 |
void main() { var even_list = [1,3,5,8,10]; print("List values before update element : ${even_list}"); even_list.replaceRange(0,3,[2,4,6]); print("List values after updating list elements : ${even_list}"); } |
Output:-
1 2 |
List values before update element : [1, 3, 5, 8, 10] List values after updating list elements : [2, 4, 6, 8, 10] |
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:-
1 |
list_name.remove(value) |
Example:-
1 2 3 4 5 6 |
void main() { var even_list = [2,4,6,8,10]; print("List values before removing element : ${even_list}"); even_list.remove(6); print("List values after removing element : ${even_list}"); } |
Output:-
1 2 |
List values before removing element : [2, 4, 6, 8, 10] List values after removing element : [2, 4, 8, 10] |
removeAt() :- The removeAt() function is used to remove an element from specified index position and returns it.
Syntax:-
1 |
list_name.removeAt(int index) |
Example:-
1 2 3 4 5 6 |
void main() { var even_list = [2,4,6,8,10]; print("List values before removing element : ${even_list}"); even_list.removeAt(3); print("List values after removing element : ${even_list}"); } |
Output:-
1 2 |
List values before removing element : [2, 4, 6, 8, 10] List values after removing element : [2, 4, 6, 10] |
removeLast() :- The removeLast() function is used to remove and returns the last element from the given list.
Syntax:-
1 |
list_name.removeLast() |
Example:-
1 2 3 4 5 6 |
void main() { var even_list = [2,4,6,8,10]; print("List values before removing element : ${even_list}"); even_list.removeLast(); print("List values after removing last element : ${even_list}"); } |
Output:-
1 2 |
List values before removing element : [2, 4, 6, 8, 10] List values after removing last element : [2, 4, 6, 8] |
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:-
1 |
list_name.removeRange(int start, int end) |
Example:-
1 2 3 4 5 6 |
void main() { var even_list = [2,4,6,8,10]; print("List values before removing element : ${even_list}"); even_list.removeRange(0,2); print("List values after removing element : ${even_list}"); } |
Output:-
1 2 |
List values before removing element : [2, 4, 6, 8, 10] List values after removing element : [6, 8, 10] |
Dart Iterating List Elements
In Dart, we can loop through the list elements using forEach method as following –
Example:-
1 2 3 4 5 6 7 |
void main() { var list = ["John", "Doe", "Smith", "Alex"]; print("W3Adda - Dart Iterating List Elements."); list.forEach((item) { print('${list.indexOf(item)}: $item'); }); } |
Output:-