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:00,750
All right.
1
2
00:00:00,780 --> 00:00:07,410
Let's talk quickly about Dart maps and how they work and how we can create them and what we can use them
2
3
00:00:07,410 --> 00:00:09,900
for. When we're talking about Dart maps
3
4
00:00:09,900 --> 00:00:16,710
it kind of refers to not like a map of the world, but more like how something can map onto something
4
5
00:00:16,710 --> 00:00:16,980
else.
5
6
00:00:17,310 --> 00:00:24,750
So for example if I type this last June longitude into Google maps, it will map onto a specific location.
6
7
00:00:25,260 --> 00:00:29,960
And even better than that, you should think of maps as almost like a phone book
7
8
00:00:29,970 --> 00:00:30,270
right?
8
9
00:00:30,330 --> 00:00:33,030
Let's say that I have three friends.
9
10
00:00:33,060 --> 00:00:34,450
I hope you have more friends than I do.
10
11
00:00:34,590 --> 00:00:40,940
Amy, James and Tim and I keep their phone numbers in a book because I'm old school like that.
11
12
00:00:41,250 --> 00:00:46,420
Now a phonebook is kind of like a very basic sort of map.
12
13
00:00:46,500 --> 00:00:49,980
It's able to associate two pieces of data together.
13
14
00:00:49,980 --> 00:00:56,260
So usually you would have a key, in this case it'd would be the names of my friends,
14
15
00:00:56,430 --> 00:01:03,640
and then you would have some values and these would be the values that are associated with the keys.
15
16
00:01:03,660 --> 00:01:09,480
This is how maps are. They're kind of like a collection type just like lists which we saw earlier.
16
17
00:01:09,870 --> 00:01:17,070
But unlike lists, maps are unordered. So they don't go from zero to one to two because it doesn't make
17
18
00:01:17,070 --> 00:01:23,670
sense to order your names in your phone book unless you're super OCD and you really want them to be
18
19
00:01:23,670 --> 00:01:29,520
alphabetized and you would rewrite your phonebook every single time you had a friend that had a name
19
20
00:01:29,520 --> 00:01:38,300
beginning with A. But essentially all that maps are is we have a key and we have a value and the key
20
21
00:01:38,330 --> 00:01:45,750
will map onto a value and we can pull out the value if we know the key. In code,
21
22
00:01:45,780 --> 00:01:48,990
what this would look like is something like this.
22
23
00:01:48,990 --> 00:01:51,860
So they would be contained inside a set of curly braces.
23
24
00:01:51,930 --> 00:01:53,490
We have a key
24
25
00:01:53,490 --> 00:02:00,780
and then after a colon we have the value that's associated with that key. And all of these entries are
25
26
00:02:00,780 --> 00:02:03,690
separated by a comma.
26
27
00:02:03,690 --> 00:02:06,110
So this is how we would create it
27
28
00:02:06,150 --> 00:02:15,150
and this is how we would use it. If we wanted to create a map, we can specify the data type as a map data
28
29
00:02:15,150 --> 00:02:21,240
type, and then we can have a set of angle brackets and just like when we had lists where we put in the
29
30
00:02:21,240 --> 00:02:24,780
data type all of the values that are in the list,
30
31
00:02:24,780 --> 00:02:31,650
in this case we can specify the data type of the keys and the data type of the values so that when we
31
32
00:02:31,650 --> 00:02:38,700
create our map or when we try to add values to our map our compiler can check to make sure that the key fits
32
33
00:02:38,820 --> 00:02:42,720
the initial key type and the value fits the value type.
33
34
00:02:42,720 --> 00:02:46,740
And if we want to use our map we simply refer to the name of the map,
34
35
00:02:46,920 --> 00:02:54,210
use some square brackets and provide a key. And this will end up giving us the value that's associated
35
36
00:02:54,210 --> 00:02:56,710
with that key.
36
37
00:02:56,880 --> 00:02:59,880
So let's create a new map from scratch.
37
38
00:02:59,940 --> 00:03:06,390
I'm going to start out by providing the data type which is going to be a map data type. And I could just
38
39
00:03:06,390 --> 00:03:12,590
leave it at that and get Dart to figure out what are the data types of the entries in the map.
39
40
00:03:12,630 --> 00:03:18,330
But if I wanted to help it along and also help other programmers who I'm working with to be able to
40
41
00:03:18,330 --> 00:03:24,960
see at a glance what is contained inside this particular map, then I could add some angle brackets. And
41
42
00:03:24,960 --> 00:03:31,860
inside, I could provide the data types of my key first which in my case is going to be a string, the names
42
43
00:03:31,860 --> 00:03:35,610
of my friends. And then the value is going to be an integer,
43
44
00:03:35,640 --> 00:03:38,460
the phone numbers of those people.
44
45
00:03:38,670 --> 00:03:44,530
And so this is how that full data type looks like. I'm creating a collection of type map
45
46
00:03:44,580 --> 00:03:51,750
and it has a key that will be string, and it'll have associated values which will be an integer. And
46
47
00:03:51,750 --> 00:03:53,310
then I get to give it a name.
47
48
00:03:53,310 --> 00:03:55,530
So let's call it I did a phonebook
48
49
00:03:55,530 --> 00:03:56,160
right?
49
50
00:03:56,280 --> 00:04:00,630
And it's gonna be set to equal a new map.
50
51
00:04:00,630 --> 00:04:06,810
So I'm going to open out with some curly braces and inside here is where I will specify my keys and
51
52
00:04:06,810 --> 00:04:07,950
values.
52
53
00:04:07,950 --> 00:04:11,280
Now in this case my keys have to be a string.
53
54
00:04:11,370 --> 00:04:13,330
So let's say that we create a key called
54
55
00:04:13,340 --> 00:04:13,680
I don't know,
55
56
00:04:13,740 --> 00:04:19,550
Kyle, and then we would add a colon to say that this is the end of the key.
56
57
00:04:19,650 --> 00:04:24,660
And now I'm going to write the value that's gonna be associated with Kyle, so let's say that he's at
57
58
00:04:25,110 --> 00:04:32,790
some random phone number. And then I can separate each of the entries in my collection with a comma.
58
59
00:04:32,790 --> 00:04:37,560
So I'm going to paste in the ones from our previous keynote just for time sake.
59
60
00:04:37,800 --> 00:04:45,290
And now if we cap off the end of our map with a semicolon, we can reformat our map to make it look pretty.
60
61
00:04:45,300 --> 00:04:48,680
So here we have a map of strings and integers,
61
62
00:04:48,690 --> 00:04:50,120
it's called phone book,
62
63
00:04:50,130 --> 00:04:54,480
it's contained inside a set of curly braces and it's got four entries.
63
64
00:04:54,540 --> 00:04:58,610
So now what I can do is I can start using my map.
64
65
00:04:58,650 --> 00:05:09,840
So for example if I wanted to print out the value that is in my phonebook for maybe let's say Amy right?
65
66
00:05:09,990 --> 00:05:15,540
Then I could write phonebook, add a set of square brackets denoting that I'm trying to pull something
66
67
00:05:15,570 --> 00:05:22,260
out of a collection, and then I would provide the key which obviously has to match one of the keys in
67
68
00:05:22,260 --> 00:05:22,800
my map.
68
69
00:05:22,920 --> 00:05:29,760
So let's try and get Amy's phone number printed into the console. So let's hit run and we can see that
69
70
00:05:29,760 --> 00:05:37,380
we get 123456789, which is the value that's associated with the key Amy.
70
71
00:05:37,380 --> 00:05:43,710
Now if I tried to pull out a key that doesn't actually exist in the map, let's say try to get my own
71
72
00:05:43,800 --> 00:05:50,450
number out of this phonebook, then all we'll get is null. When it tries to pull out of value for a key
72
73
00:05:50,460 --> 00:05:51,610
that doesn't exist
73
74
00:05:51,720 --> 00:05:53,040
you will get the value
74
75
00:05:53,040 --> 00:05:53,940
null.
75
76
00:05:53,970 --> 00:05:59,700
So it's easy to check against this if you wanted to see if a particular key existed. You could just see
76
77
00:06:00,030 --> 00:06:03,970
if phone book at a particular key is equal to null, do this.
77
78
00:06:03,990 --> 00:06:05,950
Else do that.
78
79
00:06:06,270 --> 00:06:09,360
Now we can also use this particular syntax,
79
80
00:06:09,360 --> 00:06:13,470
the square brackets, to add new values into our phonebook,
80
81
00:06:13,470 --> 00:06:15,120
that doesn't yet exist.
81
82
00:06:15,120 --> 00:06:20,790
So let's say I write phonebook and I'm going to add a value for my own name now.
82
83
00:06:20,790 --> 00:06:23,520
So I'm going to add phonebook for the key
83
84
00:06:23,520 --> 00:06:26,800
Angela is now going to be equal to,
84
85
00:06:26,940 --> 00:06:28,340
let's add some random numbers.
85
86
00:06:28,710 --> 00:06:32,490
And now if I tried to again print phonebook
86
87
00:06:35,650 --> 00:06:43,720
for the key Angela, you can see that this now works and I no longer get null printed in here. So the cool
87
88
00:06:43,720 --> 00:06:46,410
thing about maps is that it's unordered.
88
89
00:06:46,480 --> 00:06:52,750
It doesn't really matter at which point your entries go in because you don't need to pull them out in
89
90
00:06:52,750 --> 00:06:54,970
order, as in 0 is Kyle,
90
91
00:06:54,970 --> 00:07:01,480
1 is Amy. And instead you can actually get the values out of the map using something that's quite easy
91
92
00:07:01,480 --> 00:07:05,410
to read and easy to understand when you're looking at it at a glance.
92
93
00:07:05,410 --> 00:07:08,960
This syntax is very expressive.
93
94
00:07:08,950 --> 00:07:13,720
Now there's a number of methods that make it easy for you to use a map.
94
95
00:07:13,750 --> 00:07:20,680
So for example you could write phonebook.length to see how many entries you currently have in the
95
96
00:07:20,680 --> 00:07:21,330
phonebook.
96
97
00:07:21,370 --> 00:07:24,880
So that's four plus this one which is five.
97
98
00:07:25,120 --> 00:07:31,750
Or you could write phonebook.keys to print out all of the keys that's in the phone book. And it will
98
99
00:07:31,750 --> 00:07:37,540
print out everything it's currently got. Or you can do the same for values as well and it'll print out
99
100
00:07:37,630 --> 00:07:43,590
all the values it has. So we're going to be seeing maps a lot more in the coming lessons.
100
101
00:07:43,740 --> 00:07:51,510
And it's just a convenient way of putting items into a collection not in any particular orde,r but searchable
101
102
00:07:51,540 --> 00:07:52,530
by their keys.
10994
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.