Dart <List> notes

General format for creating a growable list:

List<String> myList = List<String>();
myList.add( 'one' );
myList.add( 'two' );
myList.add( 'three' );
myList.add( 'four' );
myList[0] = 'ZERO'; 
myList[4] = 'five'; // Error. Must use myList.add( 'Five' );
print( 'List: ' + myList.toString() );
//[ZERO, two, three, four]

Create a new list with initialized properties:

var newList = List.from( [ 1, 2, 3 ] ) ;
newList.add( 42 );
print( 'newList: ' + newList.toString() );
//[1, 2, 3, 42]

List that cannot be modified:

var unmodifiableList = List.unmodifiable( [ 1, 2, 3 ] );

Combine Lists

var list1 = [1, 2, 3];
var list2 = [4, 5];
var list3 = [6, 7, 8];
var combinedList3  = list1 + list2 + list3;
print(combinedList1);

Note: An exception is thrown if there is a null value in any of the Lists.

Accessing elements of a List

var myList = [0, 'one', 'two', 'three', 'four', 'five'];

myList.isEmpty;                       // false
myList.isNotEmpty;                    // true
myList.getRange(1, 3).toList();       // [one, two]
myList.take(3).toList()               // [0, one, two]

Remove items from List

The examples show you how to:

var myList = [0, 'one', 'two', 'three', 'four', 'five'];

// remove the item at index '3'
myList.removeAt(3);
// myList: [0, one, two, four, five]


// remove() returns false if the item does not exist in the List
bool isRemoved = myList.remove('three');
// isRemoved: false (already removed)
bool isRemoved = myList.remove( 'five' );
// isRemove: true

// remove all items which length > 3
myList.removeWhere((item) => item.toString().length > 3);
// myList: [0, one, two]

// remove all items in the List
myList.clear();

Dart find elements in List

  • check if a List contains an element or not | contains()
  • find the index of the first occurrence of an element | indexOf()
  • find the index of the last occurrence of an element | lastIndexOf()
  • find the index of the first occurrence of an element that matches a condition | indexWhere()
  • find the index of the last occurrence of an element that matches a condition | lastIndexWhere()
var myList = [0, 2, 4, 6, 8, 2, 8];
myList.contains(2);                        // true
myList.contains(5);                        // false
myList.indexOf(2);                         // 1
myList.lastIndexOf(2);                     // 5
myList.indexWhere((item) => item > 5);     // 3
myList.lastIndexWhere((item) => item > 5); // 6

Filter items for a List in Dart/Flutter

  • filter all items in List that match the condition | where()
  • get the first item in List that matches the condition | firstWhere()
  • get the last item in List that matches the condition | lastWhere()

var myList = [0, 2, 4, 6, 8, 2, 7];
myList.where((item) => item > 5).toList(); // [6, 8, 7]
myList.firstWhere((item) => item > 5); // 6
myList.lastWhere((item) => item > 5); // 7

Sort

Simple List.sort() will intelligently sort by value if possible.

var intList = [0, 5, 2, 3, 8, 17, 11];
intList.sort();
print(intList); // [0, 2, 3, 5, 8, 11, 17]

var tringList = ['vue', 'kotlin','dart', 'angular', 'flutter'];
tringList.sort();
print(tringList); //[angular, dart, flutter, kotlin, vue]

Sort on objects

See example

Sources

Dart/Flutter List Tutorial with Examples

Leave a Reply

Your email address will not be published. Required fields are marked *