Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
0
1
00:00:00,330 --> 00:00:07,380
Now in the last lesson we learned about the gesture detector and we added it as a parent to our reusable
1
2
00:00:07,380 --> 00:00:07,790
card,
2
3
00:00:07,830 --> 00:00:11,830
at the top. The ones with our male and female icons.
3
4
00:00:11,830 --> 00:00:18,470
And we created a method called updateColor that allows us to select one of these genders and be able
4
5
00:00:18,480 --> 00:00:21,920
to activate these cards by changing their color.
5
6
00:00:21,930 --> 00:00:25,100
Now at the moment our code works just fine.
6
7
00:00:25,440 --> 00:00:30,280
But it's a little bit wordy and also there's a few things that are not so great.
7
8
00:00:30,360 --> 00:00:37,320
And the first thing that comes to mind is the fact that we're passing in a integer to represent a gender.
8
9
00:00:37,680 --> 00:00:42,870
So we're saying that one is going to equal male and two is going to equal female.
9
10
00:00:42,870 --> 00:00:48,480
So that's why we're checking to see if the gender is one then that means the male card got pressed.
10
11
00:00:48,480 --> 00:00:52,260
But if the gender was two then that means the female card got pressed.
11
12
00:00:52,270 --> 00:00:57,510
Now as you can imagine, if you came back to this code in six months time or if somebody else was looking
12
13
00:00:57,510 --> 00:01:01,840
at it, they might get a little bit confused because it doesn't make a lot of sense.
13
14
00:01:01,890 --> 00:01:05,220
And even though we have a lot of comments here it's not great.
14
15
00:01:05,220 --> 00:01:08,160
So how can we solve this? In this lesson,
15
16
00:01:08,160 --> 00:01:14,130
we're going to address all of this by learning a concept in programming that is super useful which are
16
17
00:01:14,340 --> 00:01:18,070
Enums. Enums stand for enumeration.
17
18
00:01:18,150 --> 00:01:24,620
And if you look it up in a dictionary, it means the action of establishing the number of something.
18
19
00:01:24,780 --> 00:01:27,120
And that's kind of what we've been doing here right?
19
20
00:01:27,120 --> 00:01:34,620
We've been establishing that the male gender is the number one and the female gender is the number two.
20
21
00:01:34,740 --> 00:01:38,650
But this is super painful and quite confusing.
21
22
00:01:38,700 --> 00:01:43,020
But in programming, this need crops up very frequently.
22
23
00:01:43,020 --> 00:01:49,230
Say if you're trying to encode different types of cars in your app and you say that we have this thing
23
24
00:01:49,230 --> 00:01:54,630
called carType and if it's equal to one, then it's convertible, if it's equal to two then it's an SUV,
24
25
00:01:54,900 --> 00:01:57,930
if it's three then it's a hatchback.
25
26
00:01:57,930 --> 00:02:01,830
Now these numbers don't actually inherently have any meaning.
26
27
00:02:01,980 --> 00:02:07,800
And we're actually assigning it a meaning and trying to remember it down the line. And trying to force
27
28
00:02:07,860 --> 00:02:13,900
other people who we work with also to follow this convention, and that's difficult.
28
29
00:02:13,980 --> 00:02:20,030
But imagine if instead, we can actually give it a word which already has meaning.
29
30
00:02:20,040 --> 00:02:27,300
So if we said that the first one has a carType of .convertible and the second one was .SUV or
30
31
00:02:27,310 --> 00:02:29,250
third one was .hatchback.
31
32
00:02:29,250 --> 00:02:35,340
That makes a lot more sense and is a lot easier to work with and you can see at a glance in the code
32
33
00:02:35,610 --> 00:02:37,010
what it is that we're talking about
33
34
00:02:37,010 --> 00:02:40,160
when we're coding up these different types of cars.
34
35
00:02:40,590 --> 00:02:47,970
If you imagine that we already have booleans which we know to be either on or off or true or false, and
35
36
00:02:48,000 --> 00:02:50,540
is a bit like a light bulb. It's very binary right?
36
37
00:02:50,550 --> 00:02:52,920
It has two states, on and off.
37
38
00:02:52,920 --> 00:02:57,230
But what if we wanted more states, then a light bulb can hold.
38
39
00:02:57,270 --> 00:03:02,610
Well then that's where we kind of start using Enums. And I would say that you names are almost a bit
39
40
00:03:02,610 --> 00:03:05,990
like a blender where you have multiple options right?
40
41
00:03:06,000 --> 00:03:14,430
You might have a high setting, you might have a low setting, you might have a medium or a juice or a blend
41
42
00:03:14,460 --> 00:03:19,670
or alter blend, whatever it may be. And it's more than just on or off.
42
43
00:03:19,770 --> 00:03:26,610
There's many different options and that's exactly what we use enums for, when there's a lot of different
43
44
00:03:26,670 --> 00:03:31,290
options that we want to be able to encode and not just have to rely on numbers.
44
45
00:03:31,320 --> 00:03:39,240
And we can actually give each of the options a name that has meaning. An enum looks like this in Dart.
45
46
00:03:39,540 --> 00:03:44,850
We first start out with the keyword enum, to show that we're creating an enum. And then we give the
46
47
00:03:44,850 --> 00:03:45,930
enum a name.
47
48
00:03:45,930 --> 00:03:52,650
So it could be carType or it could be gender. But the naming convention for enum names are the same
48
49
00:03:52,710 --> 00:03:53,740
as class names.
49
50
00:03:53,760 --> 00:04:00,600
So there are camel cased but they start off with a capital letter. And then we have a set of curly braces
50
51
00:04:00,930 --> 00:04:03,380
that describes each of the different types.
51
52
00:04:03,390 --> 00:04:07,530
So say typeA, typeB, typeC or hatchback, coupé, convertible.
52
53
00:04:08,610 --> 00:04:14,910
And then when we actually need to use the enum, then we can simply writeEnumName. typeA or .
53
54
00:04:14,940 --> 00:04:21,180
typeB. Let's head over to a Dartpad to see this in a little bit more detail.
54
55
00:04:21,220 --> 00:04:29,920
Now let's say that we had a class of Car and previously we only had things such as number of seats or number
55
56
00:04:29,920 --> 00:04:36,630
of doors, which makes a lot of sense to use an integer for right? Five doors is literally an integer.
56
57
00:04:37,060 --> 00:04:44,320
But if we wanted to encode a carStyle then we might have to use an integer and we have to make a note
57
58
00:04:44,320 --> 00:04:51,010
to ourselves that remember that you know if this is one then that means it's a hatchback.
58
59
00:04:51,610 --> 00:04:59,230
And if it's a two then it means it's an SUV etc. which is quite painful. But it does mean that if
59
60
00:04:59,230 --> 00:05:02,010
we decided to initialize our car,
60
61
00:05:02,010 --> 00:05:10,420
say we create our constructor and we set our this.carStyle when we create the car.
61
62
00:05:10,420 --> 00:05:14,650
Then we could go into our main function and we could create a new car.
62
63
00:05:14,680 --> 00:05:17,500
So let's it's a data type of car.
63
64
00:05:17,530 --> 00:05:19,170
Let's call it myCar
64
65
00:05:19,480 --> 00:05:23,440
and it's going to be equal to a car that's constructed.
65
66
00:05:23,710 --> 00:05:31,090
And we have to give it a carStyle and let's say it's equal to an SUV.
66
67
00:05:31,120 --> 00:05:32,840
My car's an SUV.
67
68
00:05:32,840 --> 00:05:39,160
Now if you read this line of code, it makes absolutely no sense whatsoever to a human unless we add a
68
69
00:05:39,160 --> 00:05:46,880
whole bunch of comments to say that, oh actually this car style 2 means that I'm creating a new SUV.
69
70
00:05:47,210 --> 00:05:52,640
Now it's good practice to have comments that explain the logic of your thinking of what your program
70
71
00:05:52,640 --> 00:05:53,270
does.
71
72
00:05:53,270 --> 00:05:58,970
But it's not great when you need comments to be able to understand what's actually going on in a particular
72
73
00:05:58,970 --> 00:05:59,270
line.
73
74
00:05:59,720 --> 00:06:06,710
And this is a classic case where enums can come in and save the day, because if we lost all of these
74
75
00:06:06,710 --> 00:06:15,640
comments and we came along to this code two months later, then this makes no sense whatsoever. So what
75
76
00:06:15,640 --> 00:06:17,330
if we created an enum?
76
77
00:06:17,350 --> 00:06:23,260
So let's do this here outside of any other classes and we're going to use the enum keyword to say
77
78
00:06:23,260 --> 00:06:28,780
that we're creating a new enum. And the enum is going to have a name which is going to be a capitalized
78
79
00:06:28,870 --> 00:06:30,020
camel case.
79
80
00:06:30,340 --> 00:06:34,270
And we're going to call it CarType.
80
81
00:06:34,620 --> 00:06:40,530
And then we open up a set of curly braces and we add all the different car types that we might want
81
82
00:06:40,620 --> 00:06:41,400
in our program.
82
83
00:06:41,430 --> 00:06:50,900
So we might have hatchback, we might have a SUV and maybe a convertible. And let's just add a coupé
83
84
00:06:50,910 --> 00:06:51,970
just for fun.
84
85
00:06:51,990 --> 00:06:59,790
So we've now got four types of cars and they're enclosed inside an enum and now we can use these car
85
86
00:06:59,790 --> 00:07:00,450
types.
86
87
00:07:00,450 --> 00:07:07,530
So instead of having a car style that's represented by an integer, we can change the data type to a enum
87
88
00:07:07,530 --> 00:07:08,040
type,
88
89
00:07:08,100 --> 00:07:16,380
so our carType. And now when we initialize our carStyle, instead of writing 1 for coupé or 2 for
89
90
00:07:16,380 --> 00:07:20,880
hatchback, we can actually provide the enum as a named value.
90
91
00:07:20,910 --> 00:07:29,100
So we could write carType. and you can see that we're already getting all of our values in our dropdown
91
92
00:07:29,100 --> 00:07:29,630
list.
92
93
00:07:29,700 --> 00:07:32,700
So let's say that we're creating a new convertible.
93
94
00:07:32,700 --> 00:07:39,870
Then we've got myCar, which is created from the car constructor, and the property for its carStyle is
94
95
00:07:39,930 --> 00:07:42,150
a convertible carType.
95
96
00:07:42,150 --> 00:07:44,790
Now look at how much better that reads.
96
97
00:07:44,790 --> 00:07:50,520
Even if I came back to this code a million years later, well maybe I'd already be enslaved to the computers.
97
98
00:07:50,520 --> 00:07:56,500
But either way if I came back to this after a long time, this line of code still makes a lot of sense.
98
99
00:07:57,970 --> 00:08:04,870
So let's head back to our code and here's the challenge for you. Create a gender enum at the top of file.
99
100
00:08:04,960 --> 00:08:10,870
You can't create enums inside classes. So make sure it's outside of any of the other classes, maybe
100
101
00:08:10,870 --> 00:08:18,620
somewhere below the constants. And then use that gender enum inside our update color and inside our gesture
101
102
00:08:18,620 --> 00:08:23,620
detector to be able to say that, in this case it was the male card that was pressed
102
103
00:08:23,620 --> 00:08:29,200
and in this case it was the female card that was pressed so that we can process it differently inside
103
104
00:08:29,200 --> 00:08:30,920
this updateColor method.
104
105
00:08:31,000 --> 00:08:36,790
So pause the video and try to upgrade your code by using Dart enums.
105
106
00:08:39,680 --> 00:08:39,980
All right.
106
107
00:08:40,000 --> 00:08:46,270
So first things first, we're going to create our enum using the enum keyword. And we're just gonna call it
107
108
00:08:46,360 --> 00:08:51,520
Gender but we're going to make sure it's capitalized at the beginning to show that it's not the same
108
109
00:08:51,580 --> 00:08:54,140
as a variable or a constant.
109
110
00:08:54,130 --> 00:09:00,970
Now gender is going to have only two different values and it's going to be male and it's going to be
110
111
00:09:01,150 --> 00:09:02,860
female.
111
112
00:09:02,860 --> 00:09:11,290
So now we can use this enum gender.male or gender.female in our code. For example our updateColor
112
113
00:09:11,410 --> 00:09:20,530
can now instead of taking a int type input, we can have it as a gender type input so we can either leave
113
114
00:09:20,530 --> 00:09:27,550
that name as gender or if you find that a bit confusing, you can change it to selected gender, just to
114
115
00:09:27,550 --> 00:09:34,330
know that it's the name that we're giving the input. And now we can check to see that if the selected
115
116
00:09:34,660 --> 00:09:45,670
gender is equal to, not one, but gender.male, then we should do all of this but if the selected gender
116
117
00:09:46,090 --> 00:09:52,720
was instead equal to gender.female then we would do all of this.
117
118
00:09:52,720 --> 00:10:01,690
And now in our code here where we update color, instead of passing in one, we can pass in gender.male and
118
119
00:10:01,750 --> 00:10:06,060
over here we can pass in gender.female.
119
120
00:10:06,160 --> 00:10:14,230
So now look at how much better our code reads. When this card has a tap detected, we update the state
120
121
00:10:14,560 --> 00:10:22,630
by updating and update the color and we tell it that the female card was pressed. And then it goes into
121
122
00:10:22,690 --> 00:10:29,710
our updateColor method and it checks to see if the gender was male that was selected or if the selected
122
123
00:10:29,710 --> 00:10:34,780
gender was female that was selected and then we carry out all of the rest of this code.
123
124
00:10:35,320 --> 00:10:42,020
So enums are really useful when you have more than one option for a property.
124
125
00:10:42,160 --> 00:10:50,510
So things such as car types or high-low medium settings or in our case genders male female etc. It's
125
126
00:10:50,710 --> 00:10:58,600
really really useful to clean up our code and make it more expressive. But still our problem is that
126
127
00:10:58,900 --> 00:11:02,560
our code is so wordy that it's actually painful to look at.
127
128
00:11:02,830 --> 00:11:09,070
So in the next lesson, we're going to learn another thing that we get access to in Dart which are ternary
128
129
00:11:09,070 --> 00:11:15,190
operators. And we're going to clean up all of this to reduce it down to maybe one or two lines of code.
129
130
00:11:15,400 --> 00:11:18,760
So if you're ready for all of that magic, head over to the next lesson.
15086
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.