All language subtitles for 35. final vs constf

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
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
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranรฎ)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

If we restart our app here just to get rid of that error which I deliberately produced, we will see that

we now have a nice working first app.

Of course, it doesn't really make a difference which answer we choose

and after the third answer, our app breaks.

These are all things we can and we will tackle but other than that, we covered a lot of the core important

features which we need to know.

Now I want to take a step away from Flutter again and have a look at a Dart specific feature, a feature

which we already saw when we had a look at this colors thing.

There if I clicked on it, you saw that this is a class which essentially groups a couple of static

properties which are properties we can access without instantiating the class, which therefore ensures

that the class is basically like a grouping mechanism for these utility values which are provided out

of the box.

That's not the thing I want to primarily look at though, it's this const

keyword here instead,

because we saw something similar already. In our answer widget, we also have final.

Now both final and const sounds like we're creating values which can't change, right?

A constant value is probably the opposite of a variable,

it's a variable that doesn't change,

hence it's pretty constant in the value that's stored in it

but a final value also is a value that doesn't change.

So what's the difference?

Let me go to the main.dart file, here,

this questions list here

in the end is a constant if you will.

Why?

Because the values here never change.

Okay

but isn't that the same as final?

Indeed we can use final instead of var here and actually, that would be better than using var because

now we're really clear about the fact that we never intend to change these values once our program runs,

that's the important thing.

You use final if a value doesn't change from the point of time when your program runs. It can be initialized

with a value though,

for example in our question and answer widgets, the properties here are final because they are changed initially

when we assign a value here, when this gets created but they don't change thereafter.

Here in main.dart, we're creating a question and we're creating the answers

and when we select a new question, in the end Flutter reruns this build method here because we called

set state and therefore it rebuilds this tree and it rebuilds all these widgets, it creates a new object,

a new instance of the question class for example and it passes a value to that and therefore the value

that gets passed here into this question text property is kind of dynamic.

Once this question widget has been created, it never changes but when it is created, it's a different

value than for the last question.

Okay, I guess that makes sense.

So it's kind of dynamic, at runtime the final value gets logged in but at the point of time we're writing

the code,

so during development and also when our code gets compiled, the value is unclear because the question

widget can work with different question texts, only when it then runs, a certain text is logged in

and that's exactly what final means,

it's a runtime constant value. At the point of time this code executes, we log in final values but at

the point of time we're writing this code, we don't know what the final value will be,

again because it's really important, for the question widget for example, there is not a single

final value,

it depends on the concrete instance we create. It's different for the questions list here though,

we never change this list.

Yes we select different questions here when we output our questions but we don't change our question

list here,

we just get access to it to then show it somewhere else,

we get access to the question text to then show it somewhere else,

we don't change the questions list here and therefore, this is not just final but it's const,

it's constant. Hence here,

we should actually use const to make it really clear that this is compile time constant, not runtime constant

like final is but compile time constant.

Now compile time constant also implicitly means runtime constant.

If you know at the time you're writing the code what the final value will be and you know that this

value will never change, then of course it will also never change during runtime

but the value, the final value is not logged in when your code executes but already after you wrote

the code because this questions list will never change. And that is an important concept and hopefully

makes it clear why you see both final and const and how they differ.

Now besides adding const here in front of the variable name, you can also by the way add it in front

of a value and

now what's the difference?

The difference is that if you add it in front of the variable name, the variable is constant,

if you add it in front of the value, the value is constant

but isn't that the same? For that you have to understand how Dart manages your objects. All your objects,

no matter if it's a list or a widget or any other object based on any other class, all these objects

are stored in memory by Dart and what it actually stores in your variables where you think you store

the objects, what Dart actually stores in the variables are the pointers at the objects in memory,

so the addresses of the objects in memory you could say and if you consider a real life example. If you

have a house and you go to the postal office and you want to send a letter and you need to put your

address on the letter as well,

well of course you put your address on the letter and not your house.

Now this might sound like a dumb example but in the end, that's exactly what's happening here.

Instead of bringing the object itself to every place in your code that needs it which would take up

a lot of memory because you would constantly have to copy it and so on, instead of doing that,

Dart is pretty smart and stores the object only once somewhere and then only takes the address in the

different places.

It's in the end also what we do with answer question, with this function. In the end, the function

also is just a variable which holds an address to this function code you could say and therefore we

can pass this address, this pointer as I also called it, at this function, we can pass this to our widget

where we then bind it to onPressed because onPressed now has the address of the code it wants to execute

when the user clicks the button and that's not just a case for functions but for any object.

Now when we turn a variable into a constant, it implicitly also treats the value as constant

but you could do the opposite.

You could have a variable questions and still have a constant value

and now what would be the result of that?

Now what is allowed is that you assign a new value to questions,

you could set questions equal to a new list now and even if it's an empty list, this is technically a

new list,

it's a new object in memory.

It's not an old list without elements,

it's a new list,

it's a new object and I stored this new object in questions and therefore what I actually stored in

questions is the address of that new object

but what this means is that the old value of questions, which was the address pointing at the old object

is overriden. Now by the way for the rest of the course,

I will always say we store an object in a variable, we store a list in a variable even though technically

we store the address but saying it in the other way is simply easier

but that's important to understand. We had the address here,

now we're overriding it with a new address. This is allowed if questions is a variable, even if the value

here is constant.

If I change questions to be a constant however, I will get an error down there because now I'm trying

to assign a new value to a constant variable and that is not allowed.

So that is why we should use const here if we never want this to change because then we avoid dumb

mistakes like accidentally assigning a new value,

so this does not work if questions is a const. But what's about that const value now? For that, let

me give you a brief other example. Let's say we have a variable, dummy and that is a list of text where I

have

hello.

Now what I can do is I can call add on the list, add is one of the methods that's provided on the list object

and I can add a new value and therefore if I print dummy here and that reloads, I see hello Max being

printed here because I print the full list and I added an item to the list.

Important, unlike map, add does modify the original object, it does modify the original list.

So here I modified the original list by adding a new item. If I added const here,

this would not work, if I add const here and I save this, now I get an error during compilation because

it's a compile time constant. So during compilation, I get an error that I cannot add to an unmodifiable

list and that's great.

So you should always be pretty explicit about your goals in your code and if you know you have a list

where you maybe want to reuse the variable and assign a brand new value to it,

that would be allowed because it's a var but where you never want to change that object, then make the

value a constant.

If you have a list where you know the values will never change, well then make the whole variable a constant

so that both the variable and the list is protected against changes.

And if you have data where you know it will not change once it has its initial value, then make it final.

Now these were a lot of new concepts and of course, it's a bit overwhelming right now which is why we

will of course use all these things throughout the course and therefore it will become more and more natural

to work with final and const.

I found it important to introduce this right now though because I want you to learn and write clean

code right from the start and you will see final and const a lot in Flutter classes,

in other examples and also in this course.

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