All language subtitles for 10. [Dart] Arrow Functions

af Afrikaans
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
tl Filipino
fi Finnish
fr French Download
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 0 1 00:00:00,930 --> 00:00:05,850 So previously we've talked about all sorts of different functions with inputs and outputs. 1 2 00:00:06,070 --> 00:00:12,180 But right up here, we've got a function but the syntax looks a little bit unfamiliar. 2 3 00:00:12,190 --> 00:00:14,320 What is this arrow anyways? 3 4 00:00:14,320 --> 00:00:24,050 Previously we said that this line of code is equivalent to basically writing something like this. 4 5 00:00:24,160 --> 00:00:32,380 We have a function that's code in main, and inside this function, we carry out a single instruction which 5 6 00:00:32,380 --> 00:00:38,530 is to run our app. And the app that we want to run is our Xylophone app right here. 6 7 00:00:39,400 --> 00:00:48,700 And once that run app is done, then we have our semicolon to cap off the line and this is exactly the 7 8 00:00:48,700 --> 00:00:50,970 same as what we had before. 8 9 00:00:51,160 --> 00:00:55,030 But what is that arrow and what does it do? 9 10 00:00:55,030 --> 00:01:02,770 Well the arrow which we create using a equals sign and a right angle bracket, in Dart, is known as the 10 11 00:01:02,770 --> 00:01:09,610 arrow syntax. And you might hear other people referring to it as a fat arrow to distinguish it from a 11 12 00:01:09,610 --> 00:01:11,290 slim arrow, I guess. 12 13 00:01:11,290 --> 00:01:16,210 But the arrow syntax is used when the function body, 13 14 00:01:16,210 --> 00:01:24,670 so the part that's inside the curly braces right after the name of the function, with this part is only 14 15 00:01:24,760 --> 00:01:26,800 a single line of code. 15 16 00:01:29,600 --> 00:01:33,830 Heading back to a dartpad so I can show you how this works. 16 17 00:01:33,890 --> 00:01:41,900 Let's say that we have our add function which returns an integer. And all that it does is it adds the 17 18 00:01:41,900 --> 00:01:52,280 numbers 5 + 2. And then it returns 5 + 2 as the output which is going to be 7 and 18 19 00:01:52,370 --> 00:01:59,360 inside our main function, all that we do is we created integer variable which is called result, and we 19 20 00:01:59,360 --> 00:02:04,140 set it equal to the output of our add function. 20 21 00:02:04,160 --> 00:02:12,260 Now clearly at this point, if we print our result, we'll just get 7 printed in the console right 21 22 00:02:12,260 --> 00:02:14,640 here. 5 + 2 = 7. 22 23 00:02:14,750 --> 00:02:16,700 That becomes the result over here. 23 24 00:02:16,700 --> 00:02:21,800 And so result = 7, and when we print this line, this is what we get. 24 25 00:02:22,400 --> 00:02:30,470 Now we can also represent this function, because it only has a single line of instructions, 25 26 00:02:30,470 --> 00:02:39,610 we can also use our arrow syntax. So we can write int add and after the parentheses, we can add our arrow 26 27 00:02:40,230 --> 00:02:48,700 and we can omit the word return, but simply give the expression or what it needs to do. 27 28 00:02:48,710 --> 00:02:54,040 So this is exactly the same to the computer as this. 28 29 00:02:54,140 --> 00:02:59,780 And if I comment this out and I click run again, you'll see that when it calls add it's going to use this 29 30 00:02:59,780 --> 00:03:03,130 version and it does exactly the same thing. 30 31 00:03:03,170 --> 00:03:11,090 So that arrow syntax is equivalent to having the curly braces around whatever comes after. 31 32 00:03:11,090 --> 00:03:19,400 And it also is equivalent to having that return keyword in front of this line of syntax. 32 33 00:03:19,400 --> 00:03:26,330 Now remember, the arrow syntax only works if you have a single line of instructions. 33 34 00:03:26,360 --> 00:03:31,740 So for example, you can't for example have a print statement here as well. Print 34 35 00:03:32,030 --> 00:03:33,420 'I got called'. 35 36 00:03:34,490 --> 00:03:44,300 So now this function has two lines, and we can't combine that into the end of our arrow syntax. So we 36 37 00:03:44,300 --> 00:03:48,160 can't for example put it here and try to run both lines. 37 38 00:03:48,410 --> 00:03:54,160 If I go ahead and comment this out and click run, you can see that I get all sorts of errors. 38 39 00:03:54,260 --> 00:04:03,410 So I'm only allowed a single line expression. But even if I'm using the arrow syntax, I can still have 39 40 00:04:03,470 --> 00:04:05,510 inputs inside here. 40 41 00:04:05,510 --> 00:04:12,620 So for example, if I didn't want to add 5 to 2 every single time, which is a bit of a useless function 41 42 00:04:12,620 --> 00:04:21,990 if you think about it, let's try and add our n1 to our n2 and we return simply n1 + n2. 42 43 00:04:22,250 --> 00:04:29,990 Now in the arrow syntax version, we would represent this in much the same way inside the parentheses 43 44 00:04:30,050 --> 00:04:35,930 after the word add, we add our inputs in exactly the same way. 44 45 00:04:35,990 --> 00:04:43,430 So I'm just going to copy and paste it in there. And then after the arrow, we place our single line of 45 46 00:04:43,430 --> 00:04:48,380 instructions, which is basically just n1 + n2. 46 47 00:04:48,440 --> 00:04:55,240 So this also works and this is exactly the same as this line of code. 47 48 00:04:55,250 --> 00:05:01,970 So if we added our numbers let's say let's add 3 to 11, and we hit run. 48 49 00:05:01,970 --> 00:05:09,050 You can see that our function, using the arrow syntax, returns the number 14 which gets printed in here. 49 50 00:05:10,780 --> 00:05:17,020 So now if we head back to Android Studio, we can see that using the same principles, we can get rid of 50 51 00:05:17,170 --> 00:05:19,240 are curly braces. 51 52 00:05:19,240 --> 00:05:25,810 And even though in this case, this function doesn't actually return anything because it has a void data 52 53 00:05:25,810 --> 00:05:26,450 type, 53 54 00:05:26,500 --> 00:05:32,520 we can still use that arrow syntax to say that this is the body of this function. 54 55 00:05:32,530 --> 00:05:38,440 This is the single line of instructions which need to be carried out when this function gets called, 55 56 00:05:38,830 --> 00:05:40,780 which is simply to run the app 56 57 00:05:40,780 --> 00:05:43,090 that is our Xylophone app. 57 58 00:05:43,240 --> 00:05:49,300 So next time when you see this arrow syntax, you won't be confused as to what it does anymore. 58 59 00:05:49,330 --> 00:05:56,890 And after this module, hopefully you now have a good grasp of how packages work in Flutter, how we can import 59 60 00:05:57,040 --> 00:06:03,670 other people's code into our own projects and leverage the code that they've already written to give 60 61 00:06:03,700 --> 00:06:08,440 our apps more functionality without having to write a lot more code. 61 62 00:06:08,440 --> 00:06:13,980 We've also explored how functions work in all three flavors in Dart. 62 63 00:06:14,110 --> 00:06:22,900 And we've built out a music app which is created using one of these functions that we learnt about. So 63 64 00:06:22,990 --> 00:06:26,350 that's all from me and I'll see you on the next module. 7407

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.