In dart, a List is similar to an array in other languages. All elements are stored in an ordered way.
Syn:-
var list = ['a','b', 'c', 'd', 'e', 'f' ];
to understand a list clearly, The values that are placed inside the square braces ([ ]) are called elements, and the position of an element can be determined by the index (Usually the position of an element starts from the zeroth index or whole numbers).
for Eg, the beginning or zeroth position of a list can be consider like this
String zerothVal = list[0];
print(' The zeroth value of list is......${zerothVal}');
// Output = The zeroth value of list is...... a
A List is classified into two categories. They are
Growable List
Fixed length list
A list that is declared without its size and which can be altered during runtime is called Growable List.
Syn:
// Creating a list without its size.
var listGrowable = ['v1','v2','v3'];
print('List Before Adding an element $listGrowable');
// Output = List Before Adding an element [v1, v2, v3]
listGrowable.add('v4');
print('List After adding an element$listGrowable');
// Output = List After adding an element[v1, v2, v3, v4]
A list with fixed length and which can't be altered after size declaration is called fixed length list
Eg:
var fixedList = new List<int>.filled(5, 0, growable: false);
fixedList[0] = 0;
fixedList[1] = 10;
fixedList[2] = 20;
fixedList[3] = 30;
fixedList[4] = 40;
print('List Items: $fixedList');
// Output : List Items: [0, 10, 20, 30, 40]
For the same list when we tried to overload the data it'll trigger exception
Eg:
var fixedList = new List<int>.filled(2, 0, growable: false);
fixedList[0] = 0;
fixedList[1] = 10;
fixedList[2] = 20;
fixedList[3] = 30;
fixedList[4] = 40;
print('List Items: $fixedList');
// Exception : Uncaught Error: RangeError (index): Index out of range: index should be less than 2: 2
The major properties of a list are First, Last, Lenght, Reverse, Hash Code, isEmpty, is NotEmpty, Iterator, Runtime Type, and Single.
Let us discuss List properties & functions in detail, Consider a new fixed-length list to determine the properties of a list
Eg:
var listProperties = new List<int>.filled(5, 0, growable: false);
listProperties[0] = 5;
listProperties[1] = 10;
listProperties[2] = 20;
listProperties[3] = 30;
listProperties[4] = 40;
To get the first element from the list we use this property.
Eg:
print('First element of List ${ listProperties.first}');
// Output: First element of List 5
To get the last element from the list we use this property.
Eg:
print('Last element of List ${ listProperties.last}');
// Output: Last element of List 40
To get the total length of a list we use this property.
Eg:
print('Length of the List ${ listProperties.length}');
// Output: Length of List 5
For reversing the list we use this keyword
Eg:
print('Reversed List ${ listProperties.reversed}');
// Output : Reversed List (50, 40, 30, 20, 10, 5)
RuntimeType
To know the type of the declared variable
Eg:
print('Element inside list Runtime type ${ listProperties[0].runtimeType}');
When there is a single element available inside a list and we want to extract it, then we use this property
Eg:
var lst = <int>[];
lst.add(50);
print("The list has only one element: ${lst.single}");
// Output: The list has only one element: 50
If a list consists of more than one element,if we use single property it'll throws an error
E.g:var lst = <int>[];
lst.add(50);
lst.add(51);
print('Single Item in list ${listProperties.single}');
Output: Uncaught Error: Bad state: Too many elements
This property is used to determine whether a list contains elements or not, it is a conditional property. which returns a boolean value
Eg:
print('List is not empty ${listProperties.isNotEmpty}');
// Output: List is not empty true
Let us discuss methods that are available in a list in detail.
for adding a value at the end of a list.
E.g.
var lst = <int>[];
lst.add(50);
When we want to combine two lists in dart we use addAllmethod
E.g.
List lst1 = [1,2,3];
List lst2 = [4,5,6];
print('Added both the lists into lst1...${lst1}');
//Output : Added both the lists into lst1...[1, 2, 3, 4, 5, 6]
Eg:
List listVal = [1,2,3,4,5,6,7];
print('Print all elements inside the list before clearinng');
listVal.clear();
print('Clearing the list items ${listVal}');
Eg:
List sortIntList = [22,1,55,76,2,5,6,99,12,43,57,92];
print('List before sorting ${sortIntList}');
sortIntList.sort();
print('Sorting of list after using sort method... ${sortIntList}');
we employ this method when we want to replace existing with new element within the range inside a list.
Syntax: void fillRange(int start, int end, [E? fillValue])
Lets elaborate the syntax in detail.
Start denotes that starting index of a list where replacement starts from
End states end element's index of a list where replacement of values element
[fillValue] here we'll provide the corresponding value to fill within the range.
Eg:
var numbers = [1,2,3,4,5,6,7,8,9];
numbers.fillRange(1,3, 10);
print(numbers);
// Output: [1, 10, 10, 4, 5, 6, 7, 8, 9]
When we want to remove an element from the index we employ this method
Eg:
var numbers = [1,2,3,4,5,6,7,8,9];
numbers.remove(5);
print(numbers);
// Output: [1,2,3,4,5,7,8,9]
These are the major methods & properties in the list which we implement frequently. there are other methods that are available. here I'm sharing the dart developer site for reference purposes. kindly take a look into it.
https://api.dart.dev/stable/2.16.2/dart-core/List-class.html
Here is the combined example whatever the things we have discussed in this page.
void main(){
var listProperties = List<int>.filled(6, 0, growable: false);
listProperties[0] = 5;
listProperties[1] = 10;
listProperties[2] = 20;
listProperties[3] = 30;
listProperties[4] = 40;
listProperties[5] = 50;
// Printing length of List
print('Length of the List ${ listProperties.length}');
// Last element in a list
print('Last element of the List ${ listProperties.last}');
// First element in a list
print('Length of the List ${ listProperties.first}');
// Reversing a list
print('Reversed List ${ listProperties.reversed}');
// Runtime type
print('Element inside list Runtime type ${ listProperties[0].runtimeType}');
// When a list consists only one element we use Single property
var lst = <int>[];
lst.add(12);
print("The list has only one element: ${lst.single}");
// when there are multiple elements in a list it'll through this type of exception when we try to use 'Single' property
// print('Single Item in list ${listProperties.single}');
// Uncaught Error: Bad state: Too many elements
// Checking when list is not empty
print('List is not empty ${listProperties.isNotEmpty}');
// Add All method
List lst1 = [1,2,3];
List lst2 = [4,5,6];
lst1.addAll(lst2);
print('Added both the lists into lst1...$lst1');
List listVal = [1,2,3,4,5,6,7];
print('Print all elements inside the list before clearinng');
listVal.clear();
print('Clearing the list items $listVal');
List sortIntList = [22,1,55,76,2,5,6,99,12,43,57,92];
print('List before sorting $sortIntList');
sortIntList.sort();
print('Sorting of list after using sort method... $sortIntList');
List sortChar = ['Z','L', 'K', 'Q', 'W', 'A', 'S', 'B', 'C', 'H'];
print(sortChar);
sortChar.sort();
print(sortChar);
var numbers = [1,2,3,4,5,6,7,8,9];
numbers.fillRange(1,3, 10);
print(numbers);
var numberLst = [1,2,3,4,5,6,7,8,9];
numberLst.remove(5);
print(numberLst);
}