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:09,420
Now in the last lesson we learn all about Dart enums. And our enums are making our code a lot easier
1
2
00:00:09,420 --> 00:00:13,220
to read and it's starting to make a lot more sense.
2
3
00:00:13,380 --> 00:00:19,810
But there's still so much code for achieving some functionality that's actually really simple which
3
4
00:00:19,810 --> 00:00:22,020
is toggling a card's color.
4
5
00:00:22,050 --> 00:00:29,130
Why do we need so much code? In order to reduce the amount of code but still have the same logic and
5
6
00:00:29,130 --> 00:00:30,710
have the same behavior,
6
7
00:00:30,780 --> 00:00:33,030
we need to learn about something else
7
8
00:00:33,030 --> 00:00:37,530
that Dart gives us, which is the ternary operator.
8
9
00:00:37,530 --> 00:00:45,300
Now previously we saw that we could use the IF, ELSE IF, ELSE statements to be able to perform a different
9
10
00:00:45,300 --> 00:00:49,080
behavior depending on what the condition was.
10
11
00:00:49,080 --> 00:00:56,490
And we talked about it in terms of a rail track right? Deciding which way to go depending on which conditions
11
12
00:00:56,700 --> 00:00:58,300
were currently in play.
12
13
00:00:59,560 --> 00:01:05,710
Now the Dart ternary operator is actually just a bit of what we call syntactic sugar to make something
13
14
00:01:05,710 --> 00:01:14,710
that's actually very long and very wordy, say an if condition is true then do this else do this.
14
15
00:01:14,710 --> 00:01:19,570
And there's a lot of brackets, there's a lot of keywords and there's a lot of lines of code to achieve
15
16
00:01:19,600 --> 00:01:27,580
this. But if we used a Dart ternary operator instead, then it simplifies that into a single line where
16
17
00:01:27,670 --> 00:01:33,140
we check the condition, if that condition is true then we carry out this.
17
18
00:01:33,400 --> 00:01:39,740
And if the condition was false then we carry out this. And all that we need here is a question mark and
18
19
00:01:39,740 --> 00:01:45,460
a colon. If that seems a little bit magical don't worry because we're going to head over to our trusty
19
20
00:01:45,460 --> 00:01:49,030
DartPad to flesh it out and see how it works.
20
21
00:01:49,090 --> 00:01:56,500
So let's say that in our main function we have a new variable and it's of type boolean, so it can be
21
22
00:01:56,500 --> 00:01:57,780
true or false.
22
23
00:01:57,980 --> 00:01:58,980
And let's give it a name.
23
24
00:01:58,990 --> 00:02:09,220
Let's say that we're trying to track to see if jackBauerIsAwesome, which obviously is true.
24
25
00:02:09,280 --> 00:02:13,350
So if anybody doesn't watch the show 24 then just ignore me.
25
26
00:02:13,360 --> 00:02:19,720
Just pretend that this is a normal boolean variable. So you can just change this to a different variable
26
27
00:02:19,720 --> 00:02:20,200
if you want,
27
28
00:02:20,200 --> 00:02:22,500
say durianIsAwesome.
28
29
00:02:22,630 --> 00:02:24,260
It doesn't really matter.
29
30
00:02:24,340 --> 00:02:28,080
It's simply just a variable that can be true or false.
30
31
00:02:28,090 --> 00:02:33,380
Now at some point in our program maybe this gets changed to false or maybe it gets changed true,
31
32
00:02:33,430 --> 00:02:36,260
so we need to check for it using an IF statement,
32
33
00:02:36,280 --> 00:02:36,920
right?
33
34
00:02:36,970 --> 00:02:43,480
We might say something like if jackBauerIsAwesome
34
35
00:02:43,480 --> 00:02:50,300
then if that's equal to true, then maybe we would want to print
35
36
00:02:50,350 --> 00:02:51,130
'That's great.'
36
37
00:02:52,860 --> 00:02:56,560
Or else if that's not true maybe we want to print
37
38
00:02:57,780 --> 00:03:00,650
'Oh no.'
38
39
00:03:00,660 --> 00:03:06,870
And let me just make sure I've got all the quotation marks where it's needed and escape that quotation
39
40
00:03:06,870 --> 00:03:07,470
mark there.
40
41
00:03:07,950 --> 00:03:12,150
So now if we run our code, it will print
41
42
00:03:12,150 --> 00:03:16,310
that's great because jackBauerIsAwesome is equal to true.
42
43
00:03:16,560 --> 00:03:20,690
And we check to see if that is indeed the case.
43
44
00:03:20,730 --> 00:03:24,760
And so it falls into these brackets and it prints
44
45
00:03:24,780 --> 00:03:25,290
'That's great.'
45
46
00:03:25,500 --> 00:03:26,790
So this is classic
46
47
00:03:26,880 --> 00:03:35,130
IF ELSE statement behavior. Now we can cut down all of these lines of code into a single line just by
47
48
00:03:35,130 --> 00:03:41,370
using a ternary operator. And in order to do that the first part that comes is the condition that we're
48
49
00:03:41,370 --> 00:03:42,330
checking for.
49
50
00:03:42,330 --> 00:03:52,500
So we're saying jackBauerIsAwesome is equal to so double equals remember checks to see if the left
50
51
00:03:52,500 --> 00:03:55,020
hand side is equal to the right hand side.
51
52
00:03:55,080 --> 00:03:59,320
And if this is true, then what we'll do is print
52
53
00:03:59,400 --> 00:04:00,340
that's great.
53
54
00:04:00,390 --> 00:04:04,890
And we add a question mark to say that this is what we're checking for.
54
55
00:04:04,890 --> 00:04:08,670
Well if that is true then we're going to print that is great.
55
56
00:04:09,360 --> 00:04:16,140
But if that is false, then we're going to add a colon to say this is what we should do
56
57
00:04:16,140 --> 00:04:17,700
if it was false.
57
58
00:04:17,790 --> 00:04:26,820
So we're going to print oh no. This line of code does exactly the same thing as all of these lines of
58
59
00:04:26,820 --> 00:04:27,170
code.
59
60
00:04:27,750 --> 00:04:34,140
And I can comment that out and hit run to just confirm to you that it's exactly what happens.
60
61
00:04:34,140 --> 00:04:42,040
And if I change this to false, then we should now be triggering this part and it will print 'Oh no'. Pretty
61
62
00:04:42,040 --> 00:04:42,720
cool right?
62
63
00:04:42,730 --> 00:04:44,420
Cutting down on a lot of code.
63
64
00:04:44,530 --> 00:04:50,290
Now the first time when you see this in the wild, it can be a little bit confusing because it's not quite
64
65
00:04:50,380 --> 00:04:54,120
as laid out as all of this. Now,
65
66
00:04:54,130 --> 00:05:01,500
we can also cut down this line even further because the computer will look for all the parts before
66
67
00:05:01,510 --> 00:05:04,810
the question mark to see if it's equal to true.
67
68
00:05:04,960 --> 00:05:08,070
So we don't actually have to check if it's equal to true.
68
69
00:05:08,230 --> 00:05:10,960
It's the same as in here in the IF statement.
69
70
00:05:10,960 --> 00:05:16,960
We don't actually need that equality check. As long as this has a value of true, then it will trigger
70
71
00:05:16,990 --> 00:05:23,670
this part, but if it has a value of false it will trigger this part. Now you always want to avoid playing
71
72
00:05:23,670 --> 00:05:31,320
code golf where the only goal is to cut down on the number of lines of code and to make things as short
72
73
00:05:31,410 --> 00:05:34,140
in the number of characters as physically possible.
73
74
00:05:34,140 --> 00:05:37,920
This is an actual game, code golf, but that's not really the goal.
74
75
00:05:37,920 --> 00:05:43,860
Most cases you want to make sure that your code is succinct but it's still expressive.
75
76
00:05:43,860 --> 00:05:46,440
It still shows what's actually going on.
76
77
00:05:46,440 --> 00:05:52,290
So whenever you're choosing between whether if I want to just cut down the lines of code or whether
77
78
00:05:52,290 --> 00:05:58,830
if I can cut it down and make it clearer and easier to read and less confusing, then that's always what
78
79
00:05:58,830 --> 00:05:59,700
you want to choose.
79
80
00:05:59,700 --> 00:06:04,130
So we're not just doing it to cut down on the number of lines of code.
80
81
00:06:04,460 --> 00:06:10,910
Now what we can also do is use the same comparatives that we can use in our IF statement using our ternary
81
82
00:06:10,910 --> 00:06:11,480
operator.
82
83
00:06:11,900 --> 00:06:16,040
So let's say that we create integer called myAge and let's say it's 12.
83
84
00:06:16,310 --> 00:06:17,630
Don't worry I'm not 12.
84
85
00:06:18,290 --> 00:06:29,760
It's fine. But if we wanted to say create a boolean that is called canBuyAlcohol, and we want to assign
85
86
00:06:29,760 --> 00:06:39,900
it a value of true or false, depending on the value of my age then we can also use a ternary operator
86
87
00:06:39,900 --> 00:06:41,540
here on the right hand side.
87
88
00:06:42,060 --> 00:06:49,140
So we could say something like if myAge is greater than 21 ?
88
89
00:06:49,140 --> 00:06:53,240
So we're going to check to see if this condition is true.
89
90
00:06:53,250 --> 00:06:58,240
Well if that is the case, then we're going to set canBuyAcohol to true.
90
91
00:06:58,260 --> 00:07:06,350
But if that is not the case, then we're gonna set it to false. This is something that the IF and ELSE statements
91
92
00:07:06,500 --> 00:07:13,610
can't do. We can't put down multiple lines of code on the right hand side of an equal sign.
92
93
00:07:13,820 --> 00:07:20,630
But because a ternary operator is simply just a one line expression, then we can in this case.
93
94
00:07:20,660 --> 00:07:30,130
So if I go ahead and print canBuyAlcohol, then you can see that while my age is equal to 12 we will
94
95
00:07:30,130 --> 00:07:37,580
get false and if I change this to a little bit larger, then you can see that we get true.
95
96
00:07:37,630 --> 00:07:44,690
So we're basically assigning this value to this variable depending on this condition.
96
97
00:07:44,770 --> 00:07:48,510
And we're doing this all by using our ternary operator.
97
98
00:07:48,550 --> 00:07:52,510
So in most cases, this is what you will see happen.
98
99
00:07:52,540 --> 00:07:56,100
So this is super useful when we need to check something
99
100
00:07:56,170 --> 00:08:03,810
and depending on that check, we give our variables a different value to work on. Back to our code,
100
101
00:08:03,930 --> 00:08:11,070
it's again challenge time. And this challenge is a little bit harder but essentially I want you to apply what
101
102
00:08:11,070 --> 00:08:20,040
you've just learned about the ternary operator and I want you to get rid of this function. Instead simply
102
103
00:08:20,040 --> 00:08:29,340
use a ternary operator to check in place right here where we have our color and to check if the condition
103
104
00:08:29,520 --> 00:08:36,300
is that the maleCard is selected then we should give it an active color but if the female card was
104
105
00:08:36,300 --> 00:08:41,810
selected, then the color should be an inactive color and it should be the opposite over here.
105
106
00:08:42,810 --> 00:08:47,830
So we should have exactly the same functionality as what we've got right now
106
107
00:08:47,970 --> 00:08:56,040
but without the need for this function. And you might need to add some custom variables you might need
107
108
00:08:56,040 --> 00:09:01,950
to change the set state a little bit and you definitely need to use a ternary operator here to check
108
109
00:09:02,220 --> 00:09:04,980
for which value color should be.
109
110
00:09:04,980 --> 00:09:10,200
So it's a little bit of a harder challenge but I think you can do it. Pause a video and give it a go.
110
111
00:09:15,140 --> 00:09:15,460
All right.
111
112
00:09:15,490 --> 00:09:16,690
So first things first.
112
113
00:09:16,690 --> 00:09:19,390
I said we're going to delete this function.
113
114
00:09:19,390 --> 00:09:23,010
I'm just going to comment it out for now. And we won't really need this,
114
115
00:09:23,050 --> 00:09:29,620
but instead we're going to create a new variable that's going to hold a gender and it's going to be
115
116
00:09:29,620 --> 00:09:32,390
called selectedGender.
116
117
00:09:32,950 --> 00:09:37,600
And it's going to start out being null, because no gender is selected to begin with.
117
118
00:09:37,780 --> 00:09:45,010
But when the male card is pressed or when the female card is pressed, we're going to change the value
118
119
00:09:45,160 --> 00:09:52,060
of the selected gender. When this card is selected, the select agenda is going to be equal to gender.
119
120
00:09:52,120 --> 00:09:53,530
male.
120
121
00:09:53,680 --> 00:10:04,280
And when this card is selected, the selected gender is going to be gender.female. And we're able to
121
122
00:10:04,310 --> 00:10:10,610
write this expressive really clear code all because of our enum that we have.
122
123
00:10:10,610 --> 00:10:17,080
And that's how we're able to have this property that's holding a gender value rather than just an integer
123
124
00:10:17,120 --> 00:10:19,100
or something arbitrary.
124
125
00:10:19,100 --> 00:10:26,990
So now that we're able to set which gender was selected and keep track of that value using our variable
125
126
00:10:26,990 --> 00:10:32,970
called selectGender, then we can now address how we change the color of the card.
126
127
00:10:32,990 --> 00:10:40,730
So we know that we can use our ternary operator as a way of evaluating a condition and assigning a value.
127
128
00:10:42,110 --> 00:10:44,670
In this case for our male card,
128
129
00:10:44,690 --> 00:10:55,250
we could check to see if the selectedGender is equal to gender.male. Then in that case the color of
129
130
00:10:55,250 --> 00:10:59,240
our reusableCard should be the activeCardColor.
130
131
00:10:59,480 --> 00:11:07,700
But if this was not the case i.e. if selectedGender equals male is false, then we should change it to
131
132
00:11:07,700 --> 00:11:13,650
the inactiveCardColor. And in our reusableCard for our female card,
132
133
00:11:13,670 --> 00:11:15,520
the opposite is true.
133
134
00:11:15,560 --> 00:11:24,560
So we're going to check to see if the selectedGender is equal to female. Then in this case, we're going
134
135
00:11:24,560 --> 00:11:33,380
to change this reusable card to have a color of the activeCardColor but otherwise i.e. if the male
135
136
00:11:33,380 --> 00:11:40,190
gender was selected and this was false, then we would change it to the inactiveCardColor.
136
137
00:11:40,190 --> 00:11:44,100
Now this also has the benefit when our app first starts out,
137
138
00:11:44,210 --> 00:11:46,750
gender has no value.
138
139
00:11:46,760 --> 00:11:52,720
So both of these statements will evaluate to false. selectedGender is not gonna be equal to male
139
140
00:11:52,760 --> 00:11:55,040
and it's also not going to be equal to female.
140
141
00:11:55,370 --> 00:12:02,710
So both cards will start out with the inactiveCardColor, which is exactly what we want now.
141
142
00:12:02,730 --> 00:12:10,990
I'm going to go ahead and delete all of this code and now it's replaced by just these few lines of code.
142
143
00:12:12,010 --> 00:12:13,930
Super simple, super easy.
143
144
00:12:14,110 --> 00:12:20,440
And we're able to do this all because we're able to evaluate for an expression and assign this color
144
145
00:12:20,440 --> 00:12:27,550
property a different value depending on that condition all in the same line because we're able to use
145
146
00:12:27,670 --> 00:12:34,230
a ternary operator now instead of having to rely on IF and ELSE. Let's go ahead and test it out.
146
147
00:12:34,290 --> 00:12:41,300
So I'm going to click on hot restart so that we reset the state of our app to the very beginning.
147
148
00:12:41,310 --> 00:12:47,700
So that means that this gender, selectedGender is going to start out being null, which means both our cards
148
149
00:12:47,850 --> 00:12:53,310
are going to be inactive. But as soon as I select one of the cards, it becomes the active color.
149
150
00:12:53,790 --> 00:12:58,500
And if I select the opposite card, then it'll will deactivate that card.
150
151
00:12:58,530 --> 00:13:05,250
So we have exactly the same functionality and it's created using just five lines of code.
16863
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.