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:06,930
We know that the simplest type of Dart functions look like this. Where we have a keyword void
1
2
00:00:06,990 --> 00:00:10,600
at the beginning, we have the name of the function,
2
3
00:00:10,710 --> 00:00:13,950
we have a set of parentheses which will keep empty,
3
4
00:00:13,950 --> 00:00:16,030
and then we have our curly braces.
4
5
00:00:16,030 --> 00:00:23,040
And inside these curly braces are all of the instructions that are repeated that we need to use in different
5
6
00:00:23,040 --> 00:00:29,010
places in our code. And when we want to call this function to activate it,
6
7
00:00:29,010 --> 00:00:35,310
we call it by its name like so. Applying that to our code,
7
8
00:00:35,410 --> 00:00:41,740
let's go up here and create a new function. To start off with the void keyword, we'll call it,
8
9
00:00:41,740 --> 00:00:46,640
you call it anything you want, but let's call it changeDiceFace.
9
10
00:00:47,110 --> 00:00:50,230
And I'm going to add a set of empty parentheses.
10
11
00:00:50,230 --> 00:00:53,120
Now it's a complete building our function,
11
12
00:00:53,140 --> 00:00:56,740
all we need to add now are a set of curly braces.
12
13
00:00:57,040 --> 00:01:03,820
And it's inside these curly braces where we'll add the blocks of code which are repeated across our
13
14
00:01:03,820 --> 00:01:09,040
code namely updating our leftDiceNumber and updating our rightDiceNumber.
14
15
00:01:09,040 --> 00:01:12,580
You can see that all of this is the same.
15
16
00:01:12,580 --> 00:01:21,520
So let's cut that out of the onPressed and let's paste it into our new function. And we can now, instead
16
17
00:01:21,520 --> 00:01:28,570
of having to write all those lines of code, we can simply call it by its name, which is changeDiceFace.
17
18
00:01:28,570 --> 00:01:36,370
So now when our flat button gets pressed, the onPressed listener gets triggered and it looks to find this
18
19
00:01:36,370 --> 00:01:44,350
function with the name of changeDiceFace. It finds it right here and it calls set state, changes the
19
20
00:01:44,350 --> 00:01:46,780
leftDiceNumber, changes the rightDiceNumber.
20
21
00:01:47,290 --> 00:01:53,570
So now we can put that in here as well and we're gonna call changeDiceFace down here too.
21
22
00:01:53,650 --> 00:01:57,650
And now this code is no longer repeated in two places.
22
23
00:01:57,790 --> 00:02:02,970
Or maybe if you had more dice, there'd be more places that it would be needed. Now,
23
24
00:02:02,990 --> 00:02:08,850
if you were wondering why we couldn't take out the expanded widget and the repeated flat button widget
24
25
00:02:09,240 --> 00:02:14,760
or the repeated onPressed, we're going to answer that in a few modules because we need to learn a bit
25
26
00:02:14,760 --> 00:02:18,720
more about widgets before we can do that. In the next lesson,
26
27
00:02:18,720 --> 00:02:24,860
I've got one final boss level challenge for you. And it's to build an app of your own.
27
28
00:02:24,930 --> 00:02:31,770
So once you're ready, head over there and be ready to face off the end of level boss. And I'll see you
28
29
00:02:31,830 --> 00:02:32,700
on the next module.
3358
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.