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.

Dart
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.

Similar Reads

Dart Collection

There are 5 Interfaces that we have in Dart Collection as mentioned below:...

1. 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 (<>)....

2. Set Interface

Sets are one of the essential part of Dart Collections. A set is defined as an unordered collection of unique objects....

3. Map Interface

In Dart, Maps are unordered key-value pair collection that sets an associate key to the values within. To define a Map, specify the key type and the value type inside the angle brackets(<>) as shown below:...

4. Queue Interface

Queues are used to implement FIFO(First in First Out) collection. This collection can be manipulated from both ends....

5. LinkedList Interface

It is a specialized double-linked list of elements. This allows const time adding and removing at the either end also the time to increase the size is constant....

Contact Us