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 In the last lesson, we saw that we could create a variable to hold a value and insert that value inside 1
here by using that dollar sign. 2
This is called string interpolation. But if you're new to programming and you don't know what variables 3
are, what strings are, and how these things work together to produce this effect, then I want to quickly 4
spend this lesson talking about variables in Dart. 5
So if you're an experienced programmer and you know all about variables, then feel free to skip the next 6
two lessons where I talk about variables and data types and head straight to the next lesson 7
when we continue building out our dice app. But for everybody else who's with me, 8
let's head over to our slides, and let's talk more about variables in Dart. Now, 9
variables at the end of the day, it's just a container. 10
It's just a fancy way of saying a data container. 11
So we've known each other for a little while now and it might be time for me to introduce myself. My 12
name is Angela. 13
Now that's a piece of information that you might hear as a human 14
very frequently. My name is this or my name is that. Or I might tell you that I live in London and my 15
life ambition is to eat my own body weight in Sushi. 16
But if you a computer, all of this information would be best stored as a variable. So you would have a 17
variable that's called name, and you would store the value Angela in that variable. So the full line of 18
code would look something like this. Var is a keyword that says I'm creating a variable, then I give that 19
variable a name. 20
So in this case it's called myName. And I set what that variable's equal to. So in this case, it's Angela. 21
And all the rest of it such as the semicolons and the equals sign, 22
they're all just syntax. And syntax is just computer grammar. Just as we would have commas or full stops 23
or call ons in the English language, 24
Computers also have their own grammar. And this grammar differs from programming language to programming 25
language. But in Dart, this is what it looks like when you create a new variable. 26
Now what's actually happening here? 27
Well, essentially when I write the keyword var, the computer builds a box. And then it looks to see what 28
name it should give that box. 29
So this one's called myName. And then it looks at the right hand side of the equal sign to see what 30
it should put into the box. 31
In this case it's the word Angela. And the semicolon just says, 'well this is the end of my line of code. 32
So you can close the box now.' 33
So now I have this shelf and as you create more and more variables, you build up shelves full of these 34
boxes. And whenever you need one, you can go ahead and pull it off the shelf and open it to see what's 35
inside. 36
So for example, if I wanted to print the value of the variable i just created, myName, 37
this is how I would write it. 38
I would use the print function and inside the parentheses,I would tell it where I want to print. 39
And in this case, it's not actually the words 'myName' that will be printed. 40
It's the value of that variable. 41
So by running this line of code, the computer is going to pick up that box that's labeled myName from 42
the shelf. 43
It's going to open it up, see what's inside. 44
And it's going to put that into the console. 45
And in reality, what you'll see is the output from Flutter, saying the words 46
Angela. In order for you to try this out yourself and not just take my word for it, head over to dart 47
pad.dartlang.org and I'll include a link to this in the course resources as well. 48
And I want you to delete everything inside the DART file here 49
other than the void main. 50
So from our functions lesson, you know that this is a function. 51
And this is the function that acts as the entrance point to all of our DART programs. 52
And at the moment, I'm telling it to do nothing when it starts. 53
And so when I hit run, nothing happens. But if inside here, I told it to maybe create a new variable that's 54
called a variable my name. 55
And that said it equaled the string, that's Angela. 56
So strings have these single quotes around them in Dart. 57
If you come from other programming languages, you might see double quotes. 58
But this is the convention that we work with when we're writing Dart code. And then I'm going to tell 59
the computer that I'm done with my line of code by closing it off with a semicolon. 60
So now that I've created my variable, I can use it and I can use it by referring to its name which is 61
this part right here. 62
Let's go ahead and write a print statement. And inside the print statement, let's get it to print myName 63
and close off that line of code as well. 64
And now let's click Run, and you will see the word Angela which is the value of my name printed in our 65
console. 66
So now if I'm late today, I decided that I didn't really want to have the same name anymore or if I wanted 67
to change the value that's stored in my variable, I can simply reassign it. So I can say now, my name is 68
equal to Jane. 69
And what will happen is that will fetch that box that's labeled myName, it'll will take out Angela from inside 70
it and we'll put the new value inside. 71
So now myName is equal to Jane. 72
So now if we head back to our dartpad, and I change the variable my name to equal the new value right? 73
Let's say I'm now called Jack Bauer. And I go ahead and move my print from that line to this line. 74
What do you think will be printed now? 75
Will it be Angela or will it be Jack Bauer? 76
All right. 77
So hurray for those guys who got it right. Now in Dart, when you're inside a function, 78
so inside a set of curly braces, the code runs from top to bottom. 79
So at this stage if I had a print statement, then it would print out Angela because my name is still 80
equal to Angela. 81
But at this point I modify my name to make it equal to Jack Bauer. 82
So when I print it here, it's now equal to Jack Bauer. 83
And when I click run, you'll see first my name is Angela 84
will get printed, 85
and then Jack Bauer will get printed after it's been changed. 86
Now, what if I decided to change this variable to something a little bit different? 87
What if I try to make it equal a number say 123? 88
Well now you can see already, the Dart compiler's warning us that the value of type 'int' cannot be assigned 89
to a variable of type 'String'. 90
And if I click run, then everything crashes and I've got some errors inside my console telling me pretty 91
much the same thing. 92
So what is an int and why can't it be assigned to a string? 93
Well if you're interested in finding out about that, head over to the next lesson where we discuss about 94
Dart data types. 95
So hopefully I'll see you there.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.