Notes: 10. Create a List
Dart’s List documentation: [https://api.dart.dev/stable/2.14.4/dart-core/List-class.html)
In this episode, you’ll learn about a collection type that you’ll be using in all your programs - the List otherwise known as the array. In dart, a list is synonymous with an array so you can use them 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 a Flutter tutorial, just understand it is just a list.
A list lets you store an ordered list of values, like you see in this grid. Each value you store in an list is called an element.
Lists can only store values of the same type. Lists, like variables, must have a type and values being stored in Lists, must be of that type. For example, a list of string can only contain Strings. Now Dart does allows you to create lists that hold different types, but this isn’t a good idea as its easy to create mistakes. Performing an operation on the wrong type can crash your program so don’t use mixed typed list unless for a very very good reason.
Lists are also ordered. That means each element in the list has a position - called an index. Elements in lists are zero-indexed, which means the index of the first element is 0, the index of the second element is 1 and so on. The position of each element will stay the same, unless you change it later. So when would you want to use a list?
Lists are useful when you want to store items in a particular order. This can be handy if you want to sort a list of items. For example, if you were storing high scores in a game, then you might want the high score to come first, with the next-highest score 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, want to be able to quickly find that pastry.
Now that you know what list are, and why you might want to use them, let’s see how to use them in Dart.
To get started, open up a new instance of DartPad. Okay, first task. How do you make an list? Declare a new constant, and name it pastries.
const pastries =
Then to make it a list, use a pair of square brackets
const pastries = [];
and then put a comma-separated list of values inside
const pastries = ["cookie", "cupcake", "donut", "pie"];
We’re using type inference, again. We have a bunch of Strings wrapped in square brackets, so the compiler knows that this is an 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.
const List<String> pastries = ["cookie", "cupcake", "donut", "pie"];
You’ll learn more about this syntax in a later part. Just know the angled brackets indicates the type of collection. 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 an 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.
Then change the let to var and set the value to empty pair of square brackets.
var pastries = <String>[];
Now, because it’s a variable, you can use what’s known as the append 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 (add), and then the value you want to add:
pastries.add('danish');
I’ve just added a danish to the empty list! Then Dart will append your new element to the end of the list. Now, you don’t have to add just single elements to an list. You can add multiple elements all at once to your list. You already know that a collection of elements is an list, and you can add one list to another. To do that, you use the += operator.
pastries += ['cupcake', 'donut', 'pie', 'brownie'];
Here we’ve added another list to the end of the list we already had. Let’s see.
print(pastries);
Now when 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 += 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’ll learn all this, and more, in the next episode. See you there!