All right.
So in this lesson, we're going to do a deep dive on Dart lists. And we've already seen that we can create
a list just as you would create a shopping list, by adding some items to the list and they each go in,
in order.
So for example in this case, if I was creating a guest list for my upcoming party, then as I add names
to the list they will go in, in order.
And remember that when we're dealing with computers, they always start counting from zero.
So that means that the first item is actually going to be the zeroth item in the list, and then the order
goes on logically.
So when you're picking items from the list you would refer to them by their index.
So in this case Angela is at index 0,
and Katie is at index 2. And it's a little bit counterintuitive because of the fact that the list starts
from 0.
So if we were to pick the third from the list, it's actually not 1 2 3, but it's actually this
one. We can see how this works using DartPad.
Now DartPad actually allows you to share code that you've created.
So I want to share with you a little bit of code that I made earlier on to make this exercise a little
bit easier for you
so you don't have to copy out all the names. And the way that you would share a DartPad is you would
write some code, and create aGitHub gist. Now a GitHub gist, unlike your GitHub repos,
these are just a single code file, and it allows you to share snippets of code around very very easily
and it's completely free to create and to use.
So I've created this just under my account. And now when I'm viewing this gist, I can see that it's under
this address.
So if I copy everything that comes after my username and a forward slash, then I can paste it at the
end of the dartpad.dartlang.org website.
So I'm going to add a forward slash and paste that in here.
And now when I hit enter, it will load up that's just as a Dart file inside DartPad.
So I will show you this URL, so that when you go to it, this is exactly what you will see.
So we can work on the same file together.
Once you're here, then you'll notice that I've already created a list of strings called myList.
And inside the list, I've created it with some starting pieces of data.
So I've got four names in here that matches exactly what you saw earlier in the slides.
Now if I wanted to pick out the third item from my list, I would simply say my list, that's referring
to this variable called myList,
and then I would add a set of square brackets and inside I can provide an integer, so a whole number,
and this can refer to the index of the item that I want to pick out of the list.
So if I put the number 3 in here, then this is now going to be equal to the item inside my list
that is at the third position.
So zero, one, two, three.
So this should now equal Jack.
And we can confirm this by simply printing it into the console.
So I'm going to wrap a print statement around it, and hit run.
And you can see in here we're going to get Jack printed.
So it's pulled out
the third item out of the list.
You can also try and get the index by providing the piece of data.
So for example, if I was looking for Katie in my list, then I would get back that it's at index number
2. And we can do this by simply saying myList.indexOf, and we have to provide the string that
we're looking for which in this case is Katie.
And it has to be spelt exactly the same way in terms of capitalization. And now when we print this instead,
so let's comment that out and let's wrap a print statement around this that we've just created.
Now when I hit run, we're going to get 2 in the console, because this is equal to 2. Now as we saw earlier,
you can also add it to the list by simply using the .add, and then inside the parentheses you would
provide whatever it is that you want to add.
And remember that when you're adding to the list, the new item always goes at the end of the list not
at the beginning or some random. So in this case, if we were to go ahead and comment this out, and I'm
going to write myList.add and the thing that I'm going to add is a another person onto my guest
list.
And now if I print myList, then we can see what it now looks like all right.
So let's run our code, and we can see that our list consists of Angela, James, Katie, Jack and Ben. And the
console is very helpfully telling you that this is a list by showing you the data inside a square bracket.
So we can see that whenever we use add, it always adds our data right to the end
right?
But if you wanted to add your data somewhere in between,
there's also a way to do this. You can actually insert a piece of data into the list and push everything
that came afterwards to the next index.
So in this case, Ben came in here and it was inserted into position 2 which displaced the previous item
that was at position 2 to position 3. And the way that we would do this is we would write instead
of myList.add, we would instead say myList.insert. And we have to provide a index,
so where do we want to insert it in the in the index of our list? And also what is it that we want to
insert?
So Ben is going to be promoted to position 2 and of course the string that we want to insert, is the
name Ben.
So now if we had run, you can see that previously Ben got added at the end,
now Ben is going to show up at position 2. Remember our list starts from 0, 1, 2.
Now there's a whole lot of other things that you can do with lists and you can discover some of those
even as you're typing.
So when you write myList. you can see that DartPad is already suggesting all of these things
I can do using lists.
For example, I can try and get the first item in my list by simply writing myList.first.
And I could also get the last item in the list by writing .last.
And there's also loads of other things I can do like sort my list, or remove things in my list.
And if you want to learn more about all of these things that you can do with Dart lists, I'll add a link
to this
URL, which is a page on the Dart language guide where it talks about lists and all of the things
that you can do using Dart lists in more detail.
So I recommend browsing through this website and seeing, at a glance, what are the things that you can
do with lists.
But there's no need to memorize them because once you come across a actual use case, when you find the
need to use a particular functionality of the list, then you can simply search for it and you'll come
across either a piece of documentation like this or a post on Stack Overflow where you'll be able to
use it as you need to. That's all on list. In the next lesson,
we're going to get back to coding up our app and we're going to start displaying some questions.