All language subtitles for 35. final vs constf

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: 1 00:00:02,230 --> 00:00:08,750 If we restart our app here just to get rid of that error which I deliberately produced, we will see that 2 00:00:08,750 --> 00:00:11,730 we now have a nice working first app. 3 00:00:11,740 --> 00:00:14,850 Of course, it doesn't really make a difference which answer we choose 4 00:00:14,860 --> 00:00:17,630 and after the third answer, our app breaks. 5 00:00:17,710 --> 00:00:23,950 These are all things we can and we will tackle but other than that, we covered a lot of the core important 6 00:00:23,950 --> 00:00:26,150 features which we need to know. 7 00:00:26,220 --> 00:00:33,150 Now I want to take a step away from Flutter again and have a look at a Dart specific feature, a feature 8 00:00:33,150 --> 00:00:38,710 which we already saw when we had a look at this colors thing. 9 00:00:38,770 --> 00:00:44,100 There if I clicked on it, you saw that this is a class which essentially groups a couple of static 10 00:00:44,100 --> 00:00:49,770 properties which are properties we can access without instantiating the class, which therefore ensures 11 00:00:49,770 --> 00:00:55,830 that the class is basically like a grouping mechanism for these utility values which are provided out 12 00:00:55,830 --> 00:00:57,210 of the box. 13 00:00:57,240 --> 00:01:01,430 That's not the thing I want to primarily look at though, it's this const 14 00:01:01,430 --> 00:01:02,750 keyword here instead, 15 00:01:02,850 --> 00:01:10,110 because we saw something similar already. In our answer widget, we also have final. 16 00:01:10,110 --> 00:01:16,010 Now both final and const sounds like we're creating values which can't change, right? 17 00:01:16,020 --> 00:01:20,070 A constant value is probably the opposite of a variable, 18 00:01:20,130 --> 00:01:22,000 it's a variable that doesn't change, 19 00:01:22,020 --> 00:01:25,500 hence it's pretty constant in the value that's stored in it 20 00:01:25,710 --> 00:01:29,040 but a final value also is a value that doesn't change. 21 00:01:29,070 --> 00:01:30,550 So what's the difference? 22 00:01:31,600 --> 00:01:34,410 Let me go to the main.dart file, here, 23 00:01:34,430 --> 00:01:36,640 this questions list here 24 00:01:36,770 --> 00:01:40,520 in the end is a constant if you will. 25 00:01:40,520 --> 00:01:42,140 Why? 26 00:01:42,170 --> 00:01:44,980 Because the values here never change. 27 00:01:44,990 --> 00:01:45,240 Okay 28 00:01:45,260 --> 00:01:47,910 but isn't that the same as final? 29 00:01:47,930 --> 00:01:54,200 Indeed we can use final instead of var here and actually, that would be better than using var because 30 00:01:54,200 --> 00:02:00,770 now we're really clear about the fact that we never intend to change these values once our program runs, 31 00:02:01,400 --> 00:02:02,890 that's the important thing. 32 00:02:02,930 --> 00:02:12,300 You use final if a value doesn't change from the point of time when your program runs. It can be initialized 33 00:02:12,420 --> 00:02:13,730 with a value though, 34 00:02:13,800 --> 00:02:21,650 for example in our question and answer widgets, the properties here are final because they are changed initially 35 00:02:21,720 --> 00:02:27,910 when we assign a value here, when this gets created but they don't change thereafter. 36 00:02:27,930 --> 00:02:32,160 Here in main.dart, we're creating a question and we're creating the answers 37 00:02:32,160 --> 00:02:38,180 and when we select a new question, in the end Flutter reruns this build method here because we called 38 00:02:38,190 --> 00:02:44,160 set state and therefore it rebuilds this tree and it rebuilds all these widgets, it creates a new object, 39 00:02:44,180 --> 00:02:50,220 a new instance of the question class for example and it passes a value to that and therefore the value 40 00:02:50,250 --> 00:02:55,920 that gets passed here into this question text property is kind of dynamic. 41 00:02:56,000 --> 00:03:02,520 Once this question widget has been created, it never changes but when it is created, it's a different 42 00:03:02,520 --> 00:03:04,810 value than for the last question. 43 00:03:04,830 --> 00:03:06,950 Okay, I guess that makes sense. 44 00:03:07,050 --> 00:03:14,550 So it's kind of dynamic, at runtime the final value gets logged in but at the point of time we're writing 45 00:03:14,550 --> 00:03:15,060 the code, 46 00:03:15,060 --> 00:03:21,300 so during development and also when our code gets compiled, the value is unclear because the question 47 00:03:21,320 --> 00:03:28,240 widget can work with different question texts, only when it then runs, a certain text is logged in 48 00:03:28,410 --> 00:03:30,370 and that's exactly what final means, 49 00:03:30,420 --> 00:03:38,160 it's a runtime constant value. At the point of time this code executes, we log in final values but at 50 00:03:38,160 --> 00:03:42,510 the point of time we're writing this code, we don't know what the final value will be, 51 00:03:42,600 --> 00:03:47,880 again because it's really important, for the question widget for example, there is not a single 52 00:03:47,880 --> 00:03:48,960 final value, 53 00:03:48,960 --> 00:03:55,780 it depends on the concrete instance we create. It's different for the questions list here though, 54 00:03:56,120 --> 00:03:58,400 we never change this list. 55 00:03:58,430 --> 00:04:04,130 Yes we select different questions here when we output our questions but we don't change our question 56 00:04:04,130 --> 00:04:04,860 list here, 57 00:04:04,880 --> 00:04:07,650 we just get access to it to then show it somewhere else, 58 00:04:07,700 --> 00:04:10,520 we get access to the question text to then show it somewhere else, 59 00:04:10,550 --> 00:04:16,730 we don't change the questions list here and therefore, this is not just final but it's const, 60 00:04:16,850 --> 00:04:20,090 it's constant. Hence here, 61 00:04:20,090 --> 00:04:26,840 we should actually use const to make it really clear that this is compile time constant, not runtime constant 62 00:04:26,960 --> 00:04:30,350 like final is but compile time constant. 63 00:04:30,350 --> 00:04:34,880 Now compile time constant also implicitly means runtime constant. 64 00:04:35,210 --> 00:04:40,130 If you know at the time you're writing the code what the final value will be and you know that this 65 00:04:40,130 --> 00:04:44,020 value will never change, then of course it will also never change during runtime 66 00:04:44,370 --> 00:04:51,380 but the value, the final value is not logged in when your code executes but already after you wrote 67 00:04:51,410 --> 00:04:59,780 the code because this questions list will never change. And that is an important concept and hopefully 68 00:04:59,780 --> 00:05:05,810 makes it clear why you see both final and const and how they differ. 69 00:05:05,840 --> 00:05:11,690 Now besides adding const here in front of the variable name, you can also by the way add it in front 70 00:05:11,690 --> 00:05:12,810 of a value and 71 00:05:12,820 --> 00:05:14,270 now what's the difference? 72 00:05:15,600 --> 00:05:20,520 The difference is that if you add it in front of the variable name, the variable is constant, 73 00:05:20,520 --> 00:05:23,160 if you add it in front of the value, the value is constant 74 00:05:23,160 --> 00:05:30,460 but isn't that the same? For that you have to understand how Dart manages your objects. All your objects, 75 00:05:30,570 --> 00:05:37,860 no matter if it's a list or a widget or any other object based on any other class, all these objects 76 00:05:37,890 --> 00:05:44,520 are stored in memory by Dart and what it actually stores in your variables where you think you store 77 00:05:44,580 --> 00:05:51,840 the objects, what Dart actually stores in the variables are the pointers at the objects in memory, 78 00:05:51,870 --> 00:05:59,280 so the addresses of the objects in memory you could say and if you consider a real life example. If you 79 00:05:59,280 --> 00:06:04,410 have a house and you go to the postal office and you want to send a letter and you need to put your 80 00:06:04,410 --> 00:06:06,480 address on the letter as well, 81 00:06:06,480 --> 00:06:10,060 well of course you put your address on the letter and not your house. 82 00:06:10,070 --> 00:06:14,310 Now this might sound like a dumb example but in the end, that's exactly what's happening here. 83 00:06:14,340 --> 00:06:20,430 Instead of bringing the object itself to every place in your code that needs it which would take up 84 00:06:20,430 --> 00:06:25,240 a lot of memory because you would constantly have to copy it and so on, instead of doing that, 85 00:06:25,260 --> 00:06:32,100 Dart is pretty smart and stores the object only once somewhere and then only takes the address in the 86 00:06:32,100 --> 00:06:33,240 different places. 87 00:06:33,340 --> 00:06:38,620 It's in the end also what we do with answer question, with this function. In the end, the function 88 00:06:38,620 --> 00:06:43,980 also is just a variable which holds an address to this function code you could say and therefore we 89 00:06:43,980 --> 00:06:50,610 can pass this address, this pointer as I also called it, at this function, we can pass this to our widget 90 00:06:50,640 --> 00:06:56,580 where we then bind it to onPressed because onPressed now has the address of the code it wants to execute 91 00:06:56,850 --> 00:07:02,940 when the user clicks the button and that's not just a case for functions but for any object. 92 00:07:02,940 --> 00:07:09,040 Now when we turn a variable into a constant, it implicitly also treats the value as constant 93 00:07:09,210 --> 00:07:10,940 but you could do the opposite. 94 00:07:11,070 --> 00:07:15,240 You could have a variable questions and still have a constant value 95 00:07:15,660 --> 00:07:17,930 and now what would be the result of that? 96 00:07:18,000 --> 00:07:22,500 Now what is allowed is that you assign a new value to questions, 97 00:07:22,710 --> 00:07:29,470 you could set questions equal to a new list now and even if it's an empty list, this is technically a 98 00:07:29,470 --> 00:07:30,030 new list, 99 00:07:30,030 --> 00:07:31,590 it's a new object in memory. 100 00:07:31,620 --> 00:07:33,710 It's not an old list without elements, 101 00:07:33,720 --> 00:07:34,700 it's a new list, 102 00:07:34,710 --> 00:07:40,530 it's a new object and I stored this new object in questions and therefore what I actually stored in 103 00:07:40,530 --> 00:07:43,470 questions is the address of that new object 104 00:07:43,470 --> 00:07:49,140 but what this means is that the old value of questions, which was the address pointing at the old object 105 00:07:49,620 --> 00:07:53,030 is overriden. Now by the way for the rest of the course, 106 00:07:53,030 --> 00:07:58,860 I will always say we store an object in a variable, we store a list in a variable even though technically 107 00:07:58,860 --> 00:08:02,490 we store the address but saying it in the other way is simply easier 108 00:08:02,490 --> 00:08:05,070 but that's important to understand. We had the address here, 109 00:08:05,070 --> 00:08:12,380 now we're overriding it with a new address. This is allowed if questions is a variable, even if the value 110 00:08:12,380 --> 00:08:13,760 here is constant. 111 00:08:13,760 --> 00:08:19,550 If I change questions to be a constant however, I will get an error down there because now I'm trying 112 00:08:19,550 --> 00:08:23,600 to assign a new value to a constant variable and that is not allowed. 113 00:08:23,630 --> 00:08:29,180 So that is why we should use const here if we never want this to change because then we avoid dumb 114 00:08:29,240 --> 00:08:32,010 mistakes like accidentally assigning a new value, 115 00:08:32,150 --> 00:08:41,120 so this does not work if questions is a const. But what's about that const value now? For that, let 116 00:08:41,120 --> 00:08:49,610 me give you a brief other example. Let's say we have a variable, dummy and that is a list of text where I 117 00:08:49,610 --> 00:08:49,900 have 118 00:08:49,910 --> 00:08:51,970 hello. 119 00:08:52,000 --> 00:08:59,350 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 120 00:09:00,040 --> 00:09:08,540 and I can add a new value and therefore if I print dummy here and that reloads, I see hello Max being 121 00:09:08,540 --> 00:09:12,540 printed here because I print the full list and I added an item to the list. 122 00:09:12,590 --> 00:09:21,350 Important, unlike map, add does modify the original object, it does modify the original list. 123 00:09:21,350 --> 00:09:26,260 So here I modified the original list by adding a new item. If I added const here, 124 00:09:26,270 --> 00:09:31,970 this would not work, if I add const here and I save this, now I get an error during compilation because 125 00:09:31,970 --> 00:09:39,200 it's a compile time constant. So during compilation, I get an error that I cannot add to an unmodifiable 126 00:09:39,200 --> 00:09:41,520 list and that's great. 127 00:09:41,690 --> 00:09:48,140 So you should always be pretty explicit about your goals in your code and if you know you have a list 128 00:09:48,530 --> 00:09:53,660 where you maybe want to reuse the variable and assign a brand new value to it, 129 00:09:53,750 --> 00:09:59,300 that would be allowed because it's a var but where you never want to change that object, then make the 130 00:09:59,300 --> 00:10:00,780 value a constant. 131 00:10:01,070 --> 00:10:07,250 If you have a list where you know the values will never change, well then make the whole variable a constant 132 00:10:07,400 --> 00:10:11,860 so that both the variable and the list is protected against changes. 133 00:10:12,200 --> 00:10:20,780 And if you have data where you know it will not change once it has its initial value, then make it final. 134 00:10:20,780 --> 00:10:25,820 Now these were a lot of new concepts and of course, it's a bit overwhelming right now which is why we 135 00:10:25,820 --> 00:10:31,580 will of course use all these things throughout the course and therefore it will become more and more natural 136 00:10:31,670 --> 00:10:33,860 to work with final and const. 137 00:10:33,860 --> 00:10:39,290 I found it important to introduce this right now though because I want you to learn and write clean 138 00:10:39,290 --> 00:10:45,470 code right from the start and you will see final and const a lot in Flutter classes, 139 00:10:45,470 --> 00:10:47,990 in other examples and also in this course. 15244

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