Iterable Collections

This article teaches you how to use collections that implement the Iterable class—for example List and Set. Iterables are basic building blocks for all sorts of Dart applications, and you’re probably already using them, even without noticing. This article helps you make the most out of them.

Using the embedded DartPad editors, you can test your knowledge by running example code and completing exercises.

To get the most out of this article, you should have basic knowledge of Dart syntax.

This article covers the following material:

  • How to read elements of an Iterable.
  • How to check if the elements of an Iterable satisfy a condition.
  • How to filter the contents of an Iterable.
  • How to map the contents of an Iterable to a different value.

Estimated time to complete this article: 60 minutes.

What are collections?

A collection is an object that represents a group of objects, which are called elements. Iterables are a kind of collection.

A collection can be empty, or it can contain many elements. Depending on the purpose, collections can have different structures and implementations. These are some of the most common collection types:

  • List: Used to read elements by their indexes.
  • Set: Used to contain elements that can occur only once.
  • Map: Used to read elements using a key.

What is an Iterable?

An Iterable is a collection of elements that can be accessed sequentially.

In Dart, an Iterable is an abstract class, meaning that you can’t instantiate it directly. However, you can create a new Iterable by creating a new List or Set.

Both List and Set are Iterable, so they have the same methods and properties as the Iterable class.

A Map uses a different data structure internally, depending on its implementation. For example, HashMap uses a hash table in which the elements (also called values) are obtained using a key. Elements of a Map can also be read as Iterable objects by using the map’s entries or values property.

This example shows a List of int, which is also an Iterable of int:

Iterable<int> iterable = [1, 2, 3];

The difference with a List is that with the Iterable, you can’t guarantee that reading elements by index will be efficient. Iterable, as opposed to List, doesn’t have the [] operator.

For example, consider the following code, which is invalid:

Iterable<int> iterable = [1, 2, 3];

int value = iterable[1];

If you read elements with [], the compiler tells you that the operator ‘[]’ isn’t defined for the class Iterable, which means that you can’t use [index] in this case.

You can instead read elements with elementAt(), which steps through the elements of the iterable until it reaches that position.

Iterable<int> iterable = [1, 2, 3];

int value = iterable.elementAt(1);

Continue to the next section to learn more about how to access elements of an Iterable.

Reading elements

You can read the elements of an iterable sequentially, using a for-in loop.

Example: Using a for-in loop

The following example shows you how to read elements using a for-in loop.

Example: Using first and last

In some cases, you want to access only the first or the last element of an Iterable.

With the Iterable class, you can’t access the elements directly, so you can’t call iterable[0] to access the first element. Instead, you can use first, which gets the first element.

Also, with the Iterable class, you can’t use the operator [] to access the last element, but you can use the last property.

In this example you saw how to use first and last to get the first and last elements of an Iterable. It’s also possible to find the first element that satisfies a condition. The next section shows how to do that using a method called firstWhere().

Example: Using firstWhere()

You already saw that you can access the elements of an Iterable sequentially, and you can easily get the first or last element.

Now, you learn how to use firstWhere() to find the first element that satisfies certain conditions. This method requires you to pass a predicate, which is a function that returns true if the input satisfies a certain condition.

String element = iterable.firstWhere((element) => element.length > 5);

For example, if you want to find the first String that has more than 5 characters, you must pass a predicate that returns true when the element size is greater than 5.

Run the following example to see how firstWhere() works. Do you think all the functions will give the same result?

In this example, you can see three different ways to write a predicate:

As an expression: The test code has one line that uses arrow syntax (=>).

As a block: The test code has multiple lines between brackets and a return statement.

As a function: The test code is in an external function that’s passed to the firstWhere() method as a parameter.

There is no right or wrong way. Use the way that works best for you, and that makes your code easier to read and understand.

In the example, firstWhereWithOrElse() calls firstWhere() with the optional named parameter orElse, which provides an alternative when an element isn’t found. In this case, the text ‘None!’ is returned because no element satisfies the provided condition.

Let’s see implementations of list, set and map. Remember, list and set both available in python also and map available as dictionary in python.

Now this all thing also available in dart language also.