Programming in Dart: Control Flow & Collections

May 3 2022 · Dart 2.15, DartPad, DartPad

Part 1: Control Flow

06. Iterate Over Collections

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 05. Challenge: Play with For Loops Next episode: 07. Challenge: Practice Iterating

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Transcript: 06. Iterate Over Collections

Using the for-loop is pretty helpful but as you’ve seen, it’s required a bunch of setup as well as management. Often times, you’ll have a list of items and you’ll just want to loop over it to use the current item.

For instance, if you have a had a list of integers, you’d need to write a bunch of code just to access the current number in the collection.

Thankfully, we have a loop that does just the trick. It’s called a for-in loop. The for-in loop will loop over each element in a collection, giving you access to the current element. It’s actually quite easy to use.

After the for, you define a variable that will store each element during each iteration of the loop.

Next you provide the in keyword. This goes between the variable you defined and the collection that you want to iterate on.

Finally, you provide the collection that you are iterating. That’s the for-in loop. Let’s see it in action.

To get started, open up a dartpad.dev. We’ll work be creating a list of movies of science fiction movies.

var movies = ['Aliens', 'Interstellar', '2001', 'Event Horizon'];

Now we create the loop. We start with the for keyword.

for() {

}

Now we setup a temporary variable to hold the current element in the list.

for(var movie) {

}

In this case, the movie variable represents the current in the list of movies. Remember this is a for-in loop so lets add the in keyword.

for(var movie in) {

}

Now add collection after the in keyword.

for(var movie in movies) {

}

At which point, we can access the movie variable which contains the current movie. In this case, we’ll just print it out.

for(var movie in movies) {
    print(movie);
}

Run the program. Notice how easy it was to access each element in a collection. Now let’s see if we can apply this same loop to a string. Let’s create a new variable.

var sentence = 'this space for rent';

Now let’s loop through each word like we did with the string.

for (var word in sentence) {
    print(word);
}

We end up with an error. What’s going on here?

The error informs us that the string must implement something known as an iterable. An iterable is known as a class. We won’t be covering classes in this course, but it is a basic building block in creating what are known as objects. All collections are made from iterables whereas the string is not. There is a way to make the string work with a for-in loop but you’ll have to write some custom code for that.

For now, just remember that the for-in works with collections like lists, sets and maps whereas to loop through a string, you need to use a regular for-loop.