Programming in Dart: Fundamentals

Apr 26 2022 · Dart 2.15, DartPad, DartPad

Part 2: Introducing Collections & Null Safety

10. Create a List

Episode complete

Play next episode

About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 09. Introduction Next episode: 11. Operate on a List

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.

Notes: 10. Create a List

Dart’s List documentation: [

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

In this episode, you'll learn about a collection type that you'll be using in all your programs, the list, otherwise known as an array. In Dart, a list is synonymous with an array. So, you can use the terms interchangeably. For the sake of consistency, I'll refer to them as lists, but if you hear or read the word array in any Dart or Flutter tutorial, just understand that it's a list. A list lets you store an ordered list of values, like you see in this grid. Each value you store in a list is called an element. Lists can store values of the same types. Lists like variables must have a type, and values being stored in that list must be of that type. For example, a list of strings can only contain strings. Now, Dart does allow you to create a list to hold different types, but that isn't a good idea because it's easy to make mistakes. Performing an operation on a wrong type can crash your program. So, don't use mixed types unless there's a very good reason. Lists are also ordered. This means each element in the list has a position called an index. Elements in the lists are zero indexed, which means the index for the first element is zero. The index for the next element is one, and so forth. The position of each element will remain the same unless you change it later. Now, lists are useful when you want to store items in a particular order. This can be handy if you want to store a list of items. For example, if you were storing high scores in a game then you might want the highest score to come first with the next highest score to come after that, and so on. Lists can also be handy if you want to be able to get an item from a list at a particular index. For example, you might display a list of pastries in your app, and if the user taps on the third one in the list, you want to be able to quickly find that pastry. Now that you know what lists are and why you might want to use them, let's see how to use them in Dart. To get started, open a new instance of DartPad. Okay, first task, how do you make a list? Declare a new constant and name it pastries. Then to make it a list, use a pair of square brackets, and then put a comma separated list of values inside. We're using type inference again. We have a bunch of strings wrapped in square brackets so the compiler knows that this is a list of strings, but as always you can be explicit and declare the type. It will look a little strange since we're using what is known as generic syntax. You'll learn more about generic syntax as you explore Dart, just know the angled brackets indicates the type of the list. Thus, this list can only accept strings. So, we have some pastries, but what if you don't know what you want in your list yet? You can create an empty list and add elements to it, but you can only do that with a variable. Adding elements to a list is a form of mutation, and constants like variables defined with const or final, can't be mutated or changed. So, you'll have to use a variable instead. Now because it's a variable, you can use what's known as the add method to add elements to the list. You do this by stating the name of the list you want to operate on, then a dot, then the name of the method, and then the value you want to add. I've just added a danish to the empty list. Dart will append your new element to the end of the list. Now, you don't have to add just a single element to a list. You can add multiple elements all at once to your list. You already know what a collection of elements is in a list, and you can add one list to another. To do that, you use the plus equals operator. Here, we've added another list to the end of the list we've already had. Let's see. Now, run. You can see the results of the list printed out. You can see over on the right that cupcake comes after the first element you added. Again, every time you add an element or set of elements to an existing list either using append or the plus equals operator, the added elements will appear at the end of the list. Now, it's all well and good to add elements to lists, but how do you get them back out? You learn all of this and more in the next episode. See you there.