Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
0 All right. 1
So in this lesson, we're going to do a deep dive on Dart lists. And we've already seen that we can create 2
a list just as you would create a shopping list, by adding some items to the list and they each go in, 3
in order. 4
So for example in this case, if I was creating a guest list for my upcoming party, then as I add names 5
to the list they will go in, in order. 6
And remember that when we're dealing with computers, they always start counting from zero. 7
So that means that the first item is actually going to be the zeroth item in the list, and then the order 8
goes on logically. 9
So when you're picking items from the list you would refer to them by their index. 10
So in this case Angela is at index 0, 11
and Katie is at index 2. And it's a little bit counterintuitive because of the fact that the list starts 12
from 0. 13
So if we were to pick the third from the list, it's actually not 1 2 3, but it's actually this 14
one. We can see how this works using DartPad. 15
Now DartPad actually allows you to share code that you've created. 16
So I want to share with you a little bit of code that I made earlier on to make this exercise a little 17
bit easier for you 18
so you don't have to copy out all the names. And the way that you would share a DartPad is you would 19
write some code, and create aGitHub gist. Now a GitHub gist, unlike your GitHub repos, 20
these are just a single code file, and it allows you to share snippets of code around very very easily 21
and it's completely free to create and to use. 22
So I've created this just under my account. And now when I'm viewing this gist, I can see that it's under 23
this address. 24
So if I copy everything that comes after my username and a forward slash, then I can paste it at the 25
end of the dartpad.dartlang.org website. 26
So I'm going to add a forward slash and paste that in here. 27
And now when I hit enter, it will load up that's just as a Dart file inside DartPad. 28
So I will show you this URL, so that when you go to it, this is exactly what you will see. 29
So we can work on the same file together. 30
Once you're here, then you'll notice that I've already created a list of strings called myList. 31
And inside the list, I've created it with some starting pieces of data. 32
So I've got four names in here that matches exactly what you saw earlier in the slides. 33
Now if I wanted to pick out the third item from my list, I would simply say my list, that's referring 34
to this variable called myList, 35
and then I would add a set of square brackets and inside I can provide an integer, so a whole number, 36
and this can refer to the index of the item that I want to pick out of the list. 37
So if I put the number 3 in here, then this is now going to be equal to the item inside my list 38
that is at the third position. 39
So zero, one, two, three. 40
So this should now equal Jack. 41
And we can confirm this by simply printing it into the console. 42
So I'm going to wrap a print statement around it, and hit run. 43
And you can see in here we're going to get Jack printed. 44
So it's pulled out 45
the third item out of the list. 46
You can also try and get the index by providing the piece of data. 47
So for example, if I was looking for Katie in my list, then I would get back that it's at index number 48
2. And we can do this by simply saying myList.indexOf, and we have to provide the string that 49
we're looking for which in this case is Katie. 50
And it has to be spelt exactly the same way in terms of capitalization. And now when we print this instead, 51
so let's comment that out and let's wrap a print statement around this that we've just created. 52
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, 53
you can also add it to the list by simply using the .add, and then inside the parentheses you would 54
provide whatever it is that you want to add. 55
And remember that when you're adding to the list, the new item always goes at the end of the list not 56
at the beginning or some random. So in this case, if we were to go ahead and comment this out, and I'm 57
going to write myList.add and the thing that I'm going to add is a another person onto my guest 58
list. 59
And now if I print myList, then we can see what it now looks like all right. 60
So let's run our code, and we can see that our list consists of Angela, James, Katie, Jack and Ben. And the 61
console is very helpfully telling you that this is a list by showing you the data inside a square bracket. 62
So we can see that whenever we use add, it always adds our data right to the end 63
right? 64
But if you wanted to add your data somewhere in between, 65
there's also a way to do this. You can actually insert a piece of data into the list and push everything 66
that came afterwards to the next index. 67
So in this case, Ben came in here and it was inserted into position 2 which displaced the previous item 68
that was at position 2 to position 3. And the way that we would do this is we would write instead 69
of myList.add, we would instead say myList.insert. And we have to provide a index, 70
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 71
insert? 72
So Ben is going to be promoted to position 2 and of course the string that we want to insert, is the 73
name Ben. 74
So now if we had run, you can see that previously Ben got added at the end, 75
now Ben is going to show up at position 2. Remember our list starts from 0, 1, 2. 76
Now there's a whole lot of other things that you can do with lists and you can discover some of those 77
even as you're typing. 78
So when you write myList. you can see that DartPad is already suggesting all of these things 79
I can do using lists. 80
For example, I can try and get the first item in my list by simply writing myList.first. 81
And I could also get the last item in the list by writing .last. 82
And there's also loads of other things I can do like sort my list, or remove things in my list. 83
And if you want to learn more about all of these things that you can do with Dart lists, I'll add a link 84
to this 85
URL, which is a page on the Dart language guide where it talks about lists and all of the things 86
that you can do using Dart lists in more detail. 87
So I recommend browsing through this website and seeing, at a glance, what are the things that you can 88
do with lists. 89
But there's no need to memorize them because once you come across a actual use case, when you find the 90
need to use a particular functionality of the list, then you can simply search for it and you'll come 91
across either a piece of documentation like this or a post on Stack Overflow where you'll be able to 92
use it as you need to. That's all on list. In the next lesson, 93
we're going to get back to coding up our app and we're going to start displaying some questions.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.