Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
0
1
00:00:00,300 --> 00:00:07,230
So now that you've learned about all three types of functions in Dart, can you try and solve this problem
1
2
00:00:07,260 --> 00:00:11,180
that we have here and make our error go away?
2
3
00:00:11,280 --> 00:00:12,810
So if you haven't fixed it yet,
3
4
00:00:12,840 --> 00:00:17,030
pause the video and try and complete the challenge. All right.
4
5
00:00:17,050 --> 00:00:22,160
So at the moment, we've got the arrow that says the expression here is a type of void and therefore cannot
5
6
00:00:22,160 --> 00:00:23,430
be used.
6
7
00:00:23,480 --> 00:00:30,260
And the reason is because this function buildKey doesn't return anything. It outputs void which means
7
8
00:00:30,260 --> 00:00:31,400
it outputs nothing.
8
9
00:00:31,940 --> 00:00:39,140
So in order to change this, we have to change the output type and also use that return keyword to specify
9
10
00:00:39,170 --> 00:00:41,990
what needs to come out of this function.
10
11
00:00:41,990 --> 00:00:47,350
So first let's change the void to the return type, and what is going to come out of this function?
11
12
00:00:47,360 --> 00:00:54,290
Well, essentially we want to have a whole bunch of expanded widgets in each of these places where we're
12
13
00:00:54,290 --> 00:01:03,120
calling buildKey. So we can simply specify that data type of our expanded widget, so we can write expanded
13
14
00:01:03,120 --> 00:01:04,090
write here.
14
15
00:01:04,340 --> 00:01:12,320
And now this function, once it gets called, should return something that has a data type of expanded.
15
16
00:01:12,320 --> 00:01:14,090
And that's exactly what we're going to do here.
16
17
00:01:14,090 --> 00:01:15,710
We're going to write return.
17
18
00:01:15,770 --> 00:01:19,980
And the thing that we're going to be returning is this expanded widget.
18
19
00:01:20,000 --> 00:01:26,850
So now, each time we call buildKey, it will go and find where this function was created,
19
20
00:01:27,110 --> 00:01:32,600
and it will look inside the curly braces of this function to see what it needs to do.
20
21
00:01:32,600 --> 00:01:39,890
And in this case, it simply just needs to return an expanded widget which contains a flat button which
21
22
00:01:39,890 --> 00:01:41,150
has a red color
22
23
00:01:41,240 --> 00:01:44,360
and when it gets pressed, it plays note1.
23
24
00:01:44,390 --> 00:01:52,100
So let's save this and run this in our simulator. And you can see we have a whole bunch of red buttons.
24
25
00:01:52,490 --> 00:01:57,290
And very faintly, you can see where they separate from each other.
25
26
00:01:57,320 --> 00:02:01,330
So this works but this doesn't look great, right?
26
27
00:02:01,340 --> 00:02:08,630
And we've also lost all that functionality that we added previously. But we've got our function now so
27
28
00:02:08,660 --> 00:02:15,980
we can start providing an input in between these parentheses to tell our function to build a different
28
29
00:02:16,040 --> 00:02:18,410
expanded widget each time.
29
30
00:02:18,410 --> 00:02:26,630
So for the first one, we might want to build one that has a color of red, and it plays sound 1. But for
30
31
00:02:26,630 --> 00:02:32,580
the second one, we probably want it to have a different color and play a different sound.
31
32
00:02:32,630 --> 00:02:40,220
So we have our buildKey and in between the parentheses, we can specify the inputs which has a color
32
33
00:02:40,220 --> 00:02:43,270
property, so we could write colors.red.
33
34
00:02:43,490 --> 00:02:48,520
And it also has a sound number property,
34
35
00:02:48,690 --> 00:02:51,710
and this one is going to take a integer.
35
36
00:02:51,770 --> 00:02:57,370
So in this case, it's going to be the sound number 1 that's going to be played.
36
37
00:02:57,380 --> 00:03:06,170
So now the challenge is can you change the code so that we're still using this buildKey function to
37
38
00:03:06,170 --> 00:03:13,490
build each of our Xylophone keys and update the rest of this code so that we get different colors and
38
39
00:03:13,490 --> 00:03:16,690
different sounds for each of the keys that we're building?
39
40
00:03:16,910 --> 00:03:20,710
Pause the video and try to complete this challenge.
40
41
00:03:21,000 --> 00:03:21,290
All right.
41
42
00:03:21,320 --> 00:03:25,000
So first things first, let's try and make this code work first.
42
43
00:03:25,190 --> 00:03:31,240
Now in order to do this, our build key function needs to be able to accept some arguments or some inputs.
43
44
00:03:31,730 --> 00:03:37,100
And here it says well, this thing called color and this thing called sound number isn't defined.
44
45
00:03:37,100 --> 00:03:42,950
So let's go ahead and define it right here. And remember that if we want to have named parameters, then
45
46
00:03:42,950 --> 00:03:48,020
we need to enclose the inputs inside some curly braces, like this.
46
47
00:03:48,020 --> 00:03:58,150
And the first input has a data type of color, and we decide to call that property just the word color,
47
48
00:03:58,190 --> 00:04:00,230
but of course in lower case.
48
49
00:04:00,500 --> 00:04:10,300
And then the second property was called sound number, and it has a data type of integer. So now, this line
49
50
00:04:10,360 --> 00:04:12,790
no longer gives us an error here
50
51
00:04:12,970 --> 00:04:18,290
because it's now valid code. We're providing a color and we're providing a sound
51
52
00:04:18,280 --> 00:04:25,750
number. But we now need to use what we get through the input inside this buildKey function.
52
53
00:04:25,900 --> 00:04:33,610
So instead of using color.red, we're going to use the color that we get from our input. And instead
53
54
00:04:33,610 --> 00:04:39,040
of playing the sound, that's sound 1, then we're going to play the sound that is the sound
54
55
00:04:39,040 --> 00:04:39,660
number,
55
56
00:04:39,730 --> 00:04:46,870
whatever it may be that's passed in as the input. For this key, the color input is red,
56
57
00:04:46,900 --> 00:04:50,110
so that will go in here and the sound number is 1,
57
58
00:04:50,110 --> 00:04:56,290
so that will go in here. And we're going to update all of these other build keys so that we get a different
58
59
00:04:56,290 --> 00:04:58,140
number and a different sound.
59
60
00:04:58,210 --> 00:05:04,600
I'm just gonna copy that and paste it a few times. And let's just make sure that we've got 1, 2, 3
60
61
00:05:04,600 --> 00:05:13,360
4, 5, 6 and 7. And now I'm gonna change all the colors.
61
62
00:05:13,470 --> 00:05:13,740
All right.
62
63
00:05:13,740 --> 00:05:20,520
So now if we hit save, we should see all our buttons change to the corresponding color that we're using
63
64
00:05:20,520 --> 00:05:22,780
to build each key.
64
65
00:05:22,800 --> 00:05:29,700
Now all we need is to change each of these sound numbers so that when they get pressed, they play different
65
66
00:05:29,760 --> 00:05:30,450
sounds.
66
67
00:05:33,370 --> 00:05:33,690
All right.
67
68
00:05:33,720 --> 00:05:41,970
So now we've dramatically reduced the amount of code that's required to build our Xylophone app by building
68
69
00:05:42,090 --> 00:05:43,710
an expanded widget
69
70
00:05:43,890 --> 00:05:45,490
each time we need one.
70
71
00:05:45,750 --> 00:05:52,290
And because we have these inputs, we're able to customize them for each key so that they each have a
71
72
00:05:52,290 --> 00:05:55,590
different color and they eat play a different sound.
72
73
00:05:55,710 --> 00:06:01,100
And this is all thanks to the abilities of functions. In the next lesson,
73
74
00:06:01,140 --> 00:06:07,640
I want to talk about one last thing that's related to functions and it's this syntax right up here.
74
75
00:06:07,710 --> 00:06:13,410
Now previously, we kind of glossed over this until we started covering functions. But in the next lesson,
75
76
00:06:13,490 --> 00:06:15,370
I'm going to talk about this in more depth.
76
77
00:06:15,750 --> 00:06:18,600
So all of that and more, I'll see you on the next lesson.
8489
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.