Programming in Dart: Fundamentals

Apr 26 2022 · Dart 2.15, DartPad, DartPad

Part 2: Introducing Collections & Null Safety

11. Operate on a List

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: 10. Create a List Next episode: 12. Challenge: Work with Lists

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: 11. Operate on a List

So you’ve learned how to create lists, and how to append elements to them, using the append method and the += operator.

So you know how to get things into list – but how do you get them back out?

For this, we use the subscript operator. We put the square brackets behind our variable with the index element in it.

Remember, lists are zero based so to fetch the first element, you get the zero element. So what happens when you fetch the tenth element of a list that contains only three elements? Unfortunately, you crash with a index out of range error. This just means you are trying access an element of a list that doesn’t exist so you must be careful when accessing the list.

To get started, open a new instance of DartPad. We will start by creating a list of pastries.

const pastries = ['cookie', 'cupcake', 'donut', 'pie'];

Now let’s print out the first element of list. Remember, we start with zero.

print(pastries[0]);

Now we get a cookie. Okay lets get the one hundredth element which of course doesn’t exist. Let’s see our error condition.

print(pastries[100]);

And when run, there’s our error. This is known as a runtime error. This is an error that will only appear when you run the program. This is different from a compile time error which appears when you are coding. Needless to say, runtime errors are much harder to detect and requires plenty of testing.

Ranges can also refer to a subset of the elements in an list. Say you know for sure there are elements at indexes 1 through 3 of an list, and you want to grab that subset of elements and keep a reference to them in a new list. To access a range of elements, we use the getRange method. It takes two parameters - the start index and the final index.

var morePastries = pastries.sublist(1, 3);
print(morePastries);

Notice we defined it as variable as opposed to a constant. This is because when setting a constant, our constants must be compile time constants. This means, the compiler must be able to determine the results of an operation when it converts our dart code into machine code. Even though this list is marked as a constant, the method is generates its results at runtime, so it cannot be assigned as a constant.

Now, we can add and remove items to it. Let’s get rid of all the items. To do this, we called the clear method.

morePastries.clear();
print(evenmMorePastries);

Notice that we have no items in our list. If you want to find out if a list is empty, use the isEmpty property. This is a field that returns a true or false value.

print(evenMorePastries.isEmpty);

Notice that we didn’t put a parenthesis after the isEmpty. We used a property. A parenthesis indicates we are working with a method, when working with a property, we just put the name of the property. We have a few convenient properties. For instance, to get the first property, we might access the list like this:

print(pastries[0]);

But a more convenient way to write this is to use the first property.

print(pastries.first);

We also have a last property as well.

print(pastries.last);

An incredibly important property is to get the entire size of the list. For this, we use the length property.

print(pastries.length);

Often times, you’ll want to know if the list contains anything. For this, we use the contains method. The contains method returns a true or false value.

print(pastries.contains('cookie'));

How about lasagna?

print(pastries.contains('lasagna'));

Evidently not! You saw how you could add an item to a list. What if you want to insert a value somewhere in the middle of the list, at a specific index? There’s a method for that, too: it’s called “insert”. You simply call “insert” then the index you want to put it and the value you want to add. Before we do that, let’s change the pastries list into a variable.

var pastries = ['cookie', 'cupcake', 'donut', 'pie'];

Let’s add to our list. For this, we use the add method. This method will add a new entry to the end of the list.

pastries.add('brownie');
print(pastries);

And look at that! We have a new entry. Except a brownie isn’t a pasty. Let’s change it to a creme puff.

pastries[4] = 'cream puff';

Mind you, if there isn’t an element at the index position, you’ll get a runtime error. Now lets insert a tart into the pastries. For this, we use the insert method.

pastries.insert(1, 'tart');

Now we’ve added it to the pastries.

print(pastries);

If you want to remove an item, you can use the removeAt method. This actually returns the element at that position so we can store it in a variable.

var pastry = pastries.removeAt(0);
print(pastry);

Needless to say, lists have lots of methods and properties. We’ve only scratched the surface. The documentation will give a comprehensive look at what’s available. That said, keep following along with the tutorials and in time, you’ll get used to working with lists because they are everywhere.