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.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

So, you've learned how to create lists, and how to append elements to them using the append method in the plus equals operator. So, you know how to get things into a list, but how do you get them back out? For this, we use a subscript operator. We put 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 index. So, what happens when you fetch the 10th element of a list that contains only three elements? Unfortunately, you crash with an index out of range error. This just means you are trying to access an element of a list that doesn't exist, so, you must be careful when accessing lists. To get started, open a new instance of DartPad. We will start by creating a list of pastries. Now let's print out the first element of the list. Remember, we start with zero. Now we get a cookie. Okay, now let's get the 100th element, which of course doesn't exist. Let's see our error condition. And then we run. And 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 a list. Say, you know for sure that there are elements that indexes one through three of a 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. Notice, we defined it as a 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 dark code into machine code. Even though this list is marked as a constant, the method generates its results at runtime, so it cannot be assigned as a constant. Now we can add or/and remove items to it. Let's get rid of all the items. To do this, we call the clear method. Notice that we have no items in our list. If you wanna find out if a list is empty, use the isEmpty property, this is a field that returns a true or false value. Notice that we didn't put a parentheses after isEmpty, we used a property. A parentheses indicates we're working with a method. When working with a property, we just put the name of the property. We have a few convenience properties, for instance, to get the first element, we might access the list like this. But a more convenient way is to use the first property. We also have the last property as well. An incredibly important property is to get the size of the entire list. For this, we use the length property. Oftentimes, you'll want to know if a list contains anything. For this, we use the contains method. The contains method returns a true or false value. How about lasagna? Evidently not. You saw how you could add item to the list, what if you want to insert a value somewhere in the middle of the list, add a specific index? There's a method for that too, it's called insert. You simply call insert, then the index of where 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. Let's add to our list. For this, we use the add method. The method will add a new entry to the end of the list. And look at that, we have a new entry, except a brownie isn't a pastry, let's change it to a cream puff. Mind you, if there isn't an element at the index position, you'll get a runtime error. Now, let's insert a tart into the pastries. We've added it to the 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. Needless to say, lists have lots of methods and properties, we've only scratched the surface. The documentation will give you a comprehensive look at what's available. That said, keep following along with tutorials, and in time, you'll get used to working with lists, because they are everywhere.