An indexable collection of objects with a length. List basically a collection of elements which stores them sequentially.

Subclasses of this class implement different kinds of lists.

The most common kinds of lists are:

  • Fixed-length list

An error occurs when attempting to use operations that can change the length of the list.

  • Growable list

Full implementation of the API defined in this class.

The default growable list, as created by [], keeps an internal buffer, and grows that buffer when necessary. This guarantees that a sequence of add operations will each execute in amortized constant time. Setting the length directly may take time proportional to the new length, and may change the internal capacity so that a following add operation will need to immediately increase the buffer capacity. Other list implementations may have different performance behaviour.

Let’s work with both…

Fixed-size List

This lists are not future grow able. In simple words, we can’t resize these lists. We can declare fixed size lists using ‘const’ keyword.

Code 1:

void main(){
  const lst=[1,2,3,4,6];
  print(lst);
  print(lst.length);
}
output

You can specify a list with its type.

Code 2:

void main(){
// List = lst & type Integer
  final lst=<int>[1,2,3];
  print(lst);
}
output

code 3:

void main(){
// String type List, contains 3 items
  final lst=<String>[“google”,”apple”,”microsoft”];
  print(lst);
}
output

Remember: you can’t remove or even add new items into fixed sized list. See below code…

Code 4:

void main(){
  const lst=[1,2,4];
  print(lst);
  print(‘list type ‘);
  print(lst.runtimeType);
  print(‘list length ‘);
  print(lst.length);
 
  // removing items
  lst.remove(1);
  print(lst);
  print(‘after remove list length ‘);
  print(lst.length);
 
  // adding items
  lst.add(90);
  print(lst);
  print(‘after add list length ‘);
  print(lst.length);
}
output

Growable list

The default growable list, as created by [], keeps an internal buffer, and grows that buffer when necessary.

** remember: for growable list, we need to use keyword ‘var’. Let’s derive upper codes in the case…

void main(){
  var lst=[1,2,3,4,6];
  print(lst);
  print(lst.length);
}

Adding items

We can add items into growable list using methods like – add(),addAll()

Remember: using method add() we only adds one element, but using addAll() method we adds multiple elements into list.

 Let’s see below coding implementation…

Code 5:

void main(){
var a=[1,2,3,5,6];
print(‘list now ‘);
print(a);
print(‘list length -> ‘);
print(a.length);
 
// adding items
a.add(90);
print(‘list now ‘);
print(a);
print(‘list length -> ‘);
print(a.length);
}
output

code 6:

void main(){
var a=[1,2,3];
print(‘list now ‘);
print(a);
print(‘list length -> ‘);
print(a.length);
 
// adding multiple items
a.addAll({100,200,300});
print(‘list now ‘);
print(a);
print(‘list length -> ‘);
print(a.length);
}
output

Getting index

You can get index of any element of fixed-size list and growable list.

The last index of element in this list. Searches the list backwards from index start to 0.

Code 7:

void main(){
  const lst=[10,222,33,-90,78,56];
  print(‘index 0 item’);
  print(lst[0]);
  // getting index of item -90
  print(lst.indexOf(-90));
  print(lst.lastIndexOf(222));
}
output

code 8:

void main(){
  var lst=[10,222,33,-90,78,56];
  print(‘list is ‘);
  print(lst);
  print(‘item -90 index ‘);
  print(lst.indexOf(-90));
 
  lst.add(900);
  print(‘item 900 index ‘);
  print(lst.indexOf(900));
 
}
output

Removing items

To remove an element from the growable list, use remove, removeAt, removeLast, removeRange or removeWhere.

** remember: you can’t remove any operation in fixed size list.

remove methodRemoves the first occurrence of value from this list. Returns true if value was in the list, false otherwise. The list must be growable.
removeAt(index)Removes the object at position index from this list.
removeLast()Remove last item from list

Let’s implement methods one by one…

Code 9:

void main(){
  var lst=[1,2,3,6,8];
  print(lst);
  print(‘list length ‘);
  print(lst.length);
 
  // removing items
  lst.remove(2);
  print(lst);
  print(‘list length ‘);
  print(lst.length);
 
  // removing 2nd item
  lst.remove(6);
  print(lst);
  print(‘list length ‘);
  print(lst.length);
}
output

code 10:

void main(){
  var lst=[10,-20,-45,56,78,96];
  print(‘list is ‘);
  print(lst);
  // .removeAt(index) -> remove item by index from list
  lst.removeAt(1);
  print(‘list is ‘);
  print(lst);
  lst.removeAt(0);
  print(‘list is ‘);
  print(lst);
  // removeLast() -> remove last element from list
  lst.removeLast();
  print(‘list is ‘);
  print(lst);
 
}
output

code 11:

void main(){
  var numbers = <String>[‘one’, ‘two’, ‘three’, ‘four’];
numbers.removeWhere((item) => item.length == 3);
print(numbers); // [three, four]
}
output

List Other Operations

Code 12:

import ‘dart:io’;
 
void main(){
  var lst=[“windows”,”Ubuntu”,”mac”,”android”,”BSD”];
  print(‘list is ‘);
  print(lst);
  // checks list type
  print(‘list type -> ‘);
  print(lst.runtimeType);
  // an element contains ?
  if(lst.contains(“Ubuntu”)){
    print(‘item found!’);
  }
 
  // add new element nto list
  late String? itm;
  print(‘Enter New Item Name: ‘);
  itm=stdin.readLineSync();
  if (itm!=null) {
    lst.add(itm);
  }
  print(‘after insertion list becomes -> ‘);
  print(lst);
 
  // sorting list
  lst.sort();
  print(‘after sorting list becomes -> ‘);
  print(lst);
 
}
output