Member-only story

Iterables and Iterators in Dart

Suragch
7 min readSep 23, 2020

--

unsplash

Article updated for Dart 2.13 (September 2021)

Before doing some dedicated study and practice, iterables were kind of confusing to me. If you’re like I was, then this article is for you. It turns out they’re not that difficult. I’ll explain what iterables are and how they differ from iterators. I’ll also show you a real example of how to make your own iterable.

What is an iterable?

An iterable is one kind of collection in Dart. It’s a collection that you can move through sequentially one element at a time. List and Set are two common examples of iterable collections. Queue is another one, though less common.

If you look at the source code of List, you’ll see the following:

abstract class List<E> implements EfficientLengthIterable<E> { ... }

EfficientLengthIterable is itself a subclass of Iterable, a class you’ll learn more about later. So by its very definition, you can see that lists are iterables.

Next you’ll see some of the benefits of being an iterable collection.

Iterating over the elements of a collection

Being able to move sequentially through all the elements of a collection is a prerequisite for using a for-in loop.

--

--

Suragch
Suragch

Written by Suragch

Flutter and Dart developer. Twitter: @suragch1, Email: suragch@suragch.dev

No responses yet