List in Dart
List In Dart
If you want to store multiple values of related items in the same variable, you can use a list. If you have a large collection of an item of a single type then you can store it in a list. List in dart is similar to Arrays
in other programming languages. For e.g. to store name of multiple students.
How To Create List
You can create a list by specifying the initial elements of a square bracket. This is called a list literal
.
// Integer List
var ages = [10, 30, 23];
// String List
var names = ["Raj", "John", "Rocky"];
Since in ages
all the elements of list are int, so dart infers this to be list of int
data types.
Similary in names
all the elements of list are String, so dart infers this to be list of String
data types.
Types Of Lists
- Fixed Length List
- Growable List
Mostly Used
Fixed Length List
The fixed-length lists are defined with the specified length. We cannot change the size at runtime. This will create list of 5 integers with value 0.
void main() {
var list = List<int>.filled(5,0);
print(list);
}
Note: You cannot add new item to Fixed Length List
but you can change the values of list.
Growable List
A List object declared without size is termed a Growable List. The length of the growable list can be changed in runtime.
void main() {
var list1 = [210,21,22,33,44,55];
print(list1);
}
Access Item Of List
We can access list item by index
. Remember that list index always start with 0.
var list = [210,21,22,33,44,55];
print(list[0]);
Get Index By Value
You can also get index by value.
var list = [210,21,22,33,44,55];
print(list.indexOf(22));
Find The Length Of List
You can find the length of list by using .length
method.
void main(){
List<String> names = ["Raj", "John", "Rocky"];
print(names.length);
}
Note: Remember that list index
always start with 0
and length always start with 1
.
Changing Values Of List
You can also change value of list.
void main(){
List<String> names = ["Raj", "John", "Rocky"];
names[1] = "Bill";
names[2] = "Elon";
print(names);
}
Mutable And Immutable List
Mutable list means they can change after declare and immutable list means they can’t be changed after declare.
List<String> names = ["Raj", "John", "Rocky"]; // Mutable List
names[1] = "Bill"; // possible
names[2] = "Elon"; // possible
const List<String> names = ["Raj", "John", "Rocky"]; // Immutable List
names[1] = "Bill"; // not possible
names[2] = "Elon"; // not possible
List Properties
- first: It returns the first element case.
- last: It returns the last element in the list.
- isEmpty: It returns true if the list is empty.
- isNotEmpty: It returns true if the collection has at least one element.
- length: It returns the length of the list.
- reversed: It returns a list in reverse order.
- Single: It is used to check if the list has only one element and returns it.
Aceess First And Last Elements Of List
You can access the first and last element in the list by:
void main() {
List<String> drinks = ["water", "juice", "milk", "coke"];
print("First element of the list is: ${drinks.first}");
print("Last element of the list is: ${drinks.last}");
}
Check The List Is Empty Or Not
You can also check list contain any elements inside it or not. It will give result either in true
or in false
.
void main() {
List<String> drinks = ["water", "juice", "milk", "coke"];
List<int> ages = [];
print("Is drinks Empty: "+drinks.isEmpty.toString());
print("Is drinks not Empty: "+drinks.isNotEmpty.toString());
print("Is ages Empty: "+ages.isEmpty.toString());
print("Is ages not Empty: "+ages.isNotEmpty.toString());
}
Reverse List
void main() {
List<String> drinks = ["water", "juice", "milk", "coke"];
print("List in reverse: ${drinks.reversed}");
}
Adding Item To List
Dart provides four methods that are used to insert the elements into the lists. These methods are given below.
- add(): add one element at a time and returns the modified list object.
- addAll(): insert the multiple values to the given list and each value is separated by the commas and enclosed with a square bracket ([]).
- insert(): provides the facility to insert an element at specified index position.
- insertAll(): insert the multiple value at the specified index position.
Example
void main() {
var even_list = [2,4,6,8,10];
print(even_list);
even_list.add(12);
print(even_list);
}
void main() {
var even_list = [2,4,6,8,10] ;
print(even_list);
even_list.addAll([12,14,16,18]);
print(even_list);
}
void main(){
List lst = [3,4,2,5];
print(lst);
lst.insert(4,15);
print(lst);
}
void main(){
var lst = [3,4,2,5];
print(lst);
lst.insertAll(1,[6,7,10,9]);
print(lst);
}
Replace Range Of List
void main(){
var list = [10,15,20,25,30];
print("List before updation: ${list}");
list.replaceRange(0,4,[5,6,7,8]) ;
print("List after updation using replaceAll() function : ${list}");
}
Removing List Elements
- remove(): removes one element at a time from the given list.
- removeAt(): removes an element from the specified index position and returns it.
- removeLast(): remove the last element from the given list.
- removeRange(): removes the item within the specified range.
Example Of Removing List
void main(){
var list = [10,20,30,40,50];
print("List before removing element : ${list}");
list.remove(30) ;
print("List after removing element : ${list}");
}
void main(){
var list = [10,11,12,13,14];
print("List before removing element : ${list}");
list.removeAt(3) ;
print("List after removing element : ${list}");
}
void main(){
var list = [10,20,30,40,50];
print("List before removing element:${list}");
list.removeLast();
print("List after removing last element:${list}");
}
void main(){
var list = [10,20,30,40,50];
print("List before removing element:${list}");
list.removeRange(0,3);
print("List after removing range element:${list}");
}
Loops In List
You can use for loop, for each loop or any other type of loops.
void main(){
List<int> list = [10,20,30,40,50];
list.forEach((n) => print(n));
}
Multiple All Value By 2 Of All List
void main(){
List<int> list = [10,20,30,40,50];
var douledList =
list.map((n)=> n*2);
print((douledList));
}
Combine Two Or More List In Dart
You can combine two or more lists in dart by using spread
syntax.
void main(){
List<String> names = ["Raj", "John", "Rocky"];
List<String> names2 = ["Mike","Subash","Mark"];
List<String> allNames = [...names, ...names2];
print(allNames);
}
Conditions In List
You can also use conditions in List. Here sad = false
so cart doesn’t contain Beer
in it.
void main(){
bool sad = false;
var cart = [
'milk',
'ghee',
if(sad) 'Beer'
];
print(cart);
}
Where In List Dart
You can use where with list to filter specific items. Here in this example, even numbers are only filtered.
void main(){
List<int> numbers = [2,4,6,8, 10];
List<int> even = numbers.where((number)=> number.isEven).toList();
print(even);
}
Note: Choose lists if order matters. You can easily add items to the end. Searching can be slow when the list size is big.