Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
0 We know that the simplest type of Dart functions look like this. Where we have a keyword void 1
at the beginning, we have the name of the function, 2
we have a set of parentheses which will keep empty, 3
and then we have our curly braces. 4
And inside these curly braces are all of the instructions that are repeated that we need to use in different 5
places in our code. And when we want to call this function to activate it, 6
we call it by its name like so. Applying that to our code, 7
let's go up here and create a new function. To start off with the void keyword, we'll call it, 8
you call it anything you want, but let's call it changeDiceFace. 9
And I'm going to add a set of empty parentheses. 10
Now it's a complete building our function, 11
all we need to add now are a set of curly braces. 12
And it's inside these curly braces where we'll add the blocks of code which are repeated across our 13
code namely updating our leftDiceNumber and updating our rightDiceNumber. 14
You can see that all of this is the same. 15
So let's cut that out of the onPressed and let's paste it into our new function. And we can now, instead 16
of having to write all those lines of code, we can simply call it by its name, which is changeDiceFace. 17
So now when our flat button gets pressed, the onPressed listener gets triggered and it looks to find this 18
function with the name of changeDiceFace. It finds it right here and it calls set state, changes the 19
leftDiceNumber, changes the rightDiceNumber. 20
So now we can put that in here as well and we're gonna call changeDiceFace down here too. 21
And now this code is no longer repeated in two places. 22
Or maybe if you had more dice, there'd be more places that it would be needed. Now, 23
if you were wondering why we couldn't take out the expanded widget and the repeated flat button widget 24
or the repeated onPressed, we're going to answer that in a few modules because we need to learn a bit 25
more about widgets before we can do that. In the next lesson, 26
I've got one final boss level challenge for you. And it's to build an app of your own. 27
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
on the next module.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.