Dart Tutorial

Dart Lesson 27 of 102 2 min read

Dart Collections: List, Set and Map Overview

An overview of collections in Dart. Compare List, Set and Map, learn collection literals and generics, and choose the right one.

On this page

Dart has three core collection types. Picking the right one makes your code shorter and faster.

TypeOrdered?Duplicates?Find items byLiteral
ListYesAllowedPosition (index)[1, 2, 3]
SetInsertion orderNot allowedThe value itself{1, 2, 3}
MapInsertion orderKeys are uniqueKey{'a': 1}

All three at a glance #

void main() {
  // List: an ordered sequence.
  var queue = ['Asha', 'Bimal', 'Asha'];

  // Set: unique values.
  var visitors = {'Asha', 'Bimal', 'Asha'};

  // Map: look up a value by its key.
  var ages = {'Asha': 24, 'Bimal': 31};

  print(queue);
  print(visitors);
  print(ages['Bimal']);
}
[Asha, Bimal, Asha]
{Asha, Bimal}
31

Collection literals #

A literal is a collection written directly in code. Square brackets make a list. Curly braces make a set, or a map when the items are key: value pairs.

void main() {
  var list = [1, 2, 3];
  var set = {1, 2, 3};
  var map = {'one': 1, 'two': 2};

  print('${list.length} ${set.length} ${map.length}');
}
3 3 2

Empty collections need a type #

Dart cannot infer the type of an empty collection, so say it in angle brackets. Be careful: plain {} is an empty map, not a set.

void main() {
  var names = <String>[];
  var ids = <int>{};
  var stock = <String, int>{};

  names.add('Kamal');
  ids.add(7);
  stock['pen'] = 40;

  print('$names $ids $stock');
}
[Kamal] {7} {pen: 40}

The type in angle brackets is a generic type argument. A List<String> only accepts strings, so mistakes are caught before the program runs.

Things every collection can do #

void main() {
  var numbers = [5, 3, 8];

  print(numbers.length);
  print(numbers.isEmpty);
  print(numbers.isNotEmpty);
  print(numbers.contains(3));
  print(numbers.first);
  print(numbers.last);

  for (final n in numbers) {
    print(n);
  }
}
3
false
true
true
5
8
5
3
8

Converting between them #

void main() {
  var list = ['b', 'a', 'b'];

  var set = list.toSet();        // removes duplicates
  var back = set.toList();
  var indexed = list.asMap();    // index -> value

  print(set);
  print(back);
  print(indexed);
}
{b, a}
[b, a]
{0: b, 1: a, 2: b}

Which should I choose? #

  • Order matters, or the same value can appear twice: List.
  • You only care whether something is present, or you need uniqueness: Set.
  • You look things up by a name or id: Map.

Try it yourself #

For each of these, decide whether a List, Set or Map fits best, then create it: the songs in a playlist, the unique words in a sentence, and the price of each item on a menu.

Practise in the playground Updated by Santosh Adhikari