List Interface
The list is an ordered group of objects where each object is from one specific type. To define a list in dart, specify the object type inside the angled brackets (<>).
Syntax:
List<String> fruits = ["Mango", "Apple", "Banana"]
Example:
Here we have defined a list and performed some common operations along with some basic commonly used methods.
void main() {
// creating a new empty List
List geekList = new List();
// We can also create a list with a predefined type
// as List<int> sampleList = new List()
// and also define a list of fixed length as
// List<int> sampleList = new List(5)
// Adding an element to the geekList
geekList.addAll([1,2,3,4,5,"Apple"]);
print(geekList);
// Looping over the list
for(var i = 0; i<geekList.length;i++){
print("element $i is ${geekList[i]}");
}
// Removing an element from geekList by index
geekList.removeAt(2);
// Removing an element from geekList by object
geekList.remove("Apple");
print(geekList);
// Return a reversed version of the list
print(geekList.reversed);
// Checks if the list is empty
print(geekList.isEmpty);
// Gives the first element of the list
print(geekList.first);
// Reassigning the geekList and creating the
// elements using Iterable
geekList = Iterable<int>.generate(10).toList();
print(geekList);
}
Output:
[1, 2, 3, 4, 5, Apple]
element 0 is 1
element 1 is 2
element 2 is 3
element 3 is 4
element 4 is 5
element 5 is Apple
[1, 2, 4, 5]
(5, 4, 2, 1)
false
1
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Classes Associated with Set Interface
Class | Description |
---|---|
UnmodifiableListView<E> | An unmodifiable List view of another List. |
Dart – Collections
Collections are groups of objects that represent a particular element. The dart::collection library is used to implement the Collection in Dart. There are a variety of collections available in Dart.