What are collections (and iterables)?

on Monday, 20th of July, 2020

This section is heavily inspired by Dart's own codelab on the subject, it's less detailed (for beginners), but includes more methods and examples (for those looking for specific information, like Iterable.fold).

Collections are Dart objects that contain more objects, referred to as elements. For example a Map or List. An iterable collection is a collection that implements the Dart class Iterable. The most common iterables are List and Set.

A `Map` object is a collection, but not an iterable. Maps are covered in the latter part of this section, as it's often useful to _iterate_ over map properties as well.

In a nutshell, an iterable is a collection that sequential access to its elements. In plain English, an iterable stores its elements in order, and you can find the elements within the collection by its position.

In code, that looks something like this:

Iterable<String> greeting = ['Hello', 'World'];

Bracket Notation

Using the bracket notation is the just the basic way to interact with iterables.

Iterable greeting = ['Hello', 'World']; 
void main() {
    var hello = greeting[0];
    var world = greeting[1];


Like in most languages, Dart's iterables are 0-indexed, and you can access te different elements by their index.

Along with indexing, there are a variety of methods and properties on iterables that are handy, and we'll explore throughout this section.

For most of the remainder of the section, I'll be using `List`, as its the most common iterable, but most of the knowledge could be applied to all iterables, including `Set`.

Join thousands of Flutter developers.

Sign up for infrequent updates about Flutter and Dart.

You can get all this content and more in one place. Check out my new book Flutter in Action