What are collections (and iterables)?
on Monday, 20th of July, 2020
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
.
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];
print(hello);
print(world);
}
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.