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