All language subtitles for 7. More Dart Basics

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,260 --> 00:00:09,190 We had a first look at some programming basics, functions and types or some of the important types 2 00:00:09,190 --> 00:00:15,970 for the moment but there is more in programming and there are two other features which we need to understand 3 00:00:16,330 --> 00:00:19,520 before we can go back to our main.dart file. 4 00:00:19,560 --> 00:00:24,870 The first feature is the feature or the idea of variables. 5 00:00:24,870 --> 00:00:31,260 Thus far what I always did in here is, I always immediately printed the result of addNumbers. 6 00:00:31,260 --> 00:00:35,520 Now of course this is just some simple dummy code and therefore this make sense here, 7 00:00:35,520 --> 00:00:39,690 we basically just want to see what this yields us here. 8 00:00:39,720 --> 00:00:47,550 In reality however, you often write programs where you also need to store the result of some calculation 9 00:00:47,580 --> 00:00:52,730 or some user input, you want to store it somewhere so that you can use it later. 10 00:00:52,740 --> 00:00:57,900 Now with store, I don't mean in a database but in memory because you needed it later, 11 00:00:57,900 --> 00:01:00,910 a couple of code lines later in the same code. 12 00:01:01,020 --> 00:01:04,380 So we don't want to write it into a file or store it in a database, 13 00:01:04,500 --> 00:01:06,530 we just want to store it in memory 14 00:01:06,600 --> 00:01:13,830 so that when this code executes, we can take it and use it a couple of lines further down for example 15 00:01:14,310 --> 00:01:21,140 and we can store data with the help of so-called variables. Let's say that firstResult here shouldn't 16 00:01:21,140 --> 00:01:25,580 be printed immediately but instead I do want to store it in a variable. 17 00:01:25,580 --> 00:01:31,970 You create a variable by giving it any name you want, like firstResult and the naming convention here 18 00:01:31,970 --> 00:01:40,700 is just the same as for functions, you use camel case, lowercase starting character, then only one word 19 00:01:40,880 --> 00:01:45,600 and every word inside the word starts with a capital character. 20 00:01:45,650 --> 00:01:52,900 Now that's firstResult and you assign a value to that so-called variable now with an equals sign. 21 00:01:52,910 --> 00:01:57,660 Now it's called variable because the data in there is variable 22 00:01:57,670 --> 00:02:03,200 where you can change it, here I'm storing the result of addNumbers in firstResult, I could reuse that 23 00:02:04,760 --> 00:02:08,390 and now also store that result in firstResult. 24 00:02:08,420 --> 00:02:14,630 So after the first line, the result of addNumbers for 1 and 2.6 would be stored in here, after 25 00:02:14,630 --> 00:02:16,780 the second line line here, 26 00:02:16,910 --> 00:02:22,180 the result of addNumbers with 1 and 1 would be stored here and the firstResult, so this result of this 27 00:02:22,180 --> 00:02:23,540 line here would be dropped, 28 00:02:23,600 --> 00:02:28,310 we would store that result in the firstResult variable now. 29 00:02:28,370 --> 00:02:33,290 However, we have a syntax error here and that means that Dart doesn't understand this. 30 00:02:33,380 --> 00:02:39,760 This is almost the right syntax but to tell Dart that this is a variable, you need to add the var keyword 31 00:02:39,770 --> 00:02:40,140 here 32 00:02:40,160 --> 00:02:46,430 when you create that variable for the first time. So not in this line where I then use it again, the same 33 00:02:46,430 --> 00:02:47,700 variable with the same name 34 00:02:47,780 --> 00:02:55,040 but when you introduce it, then you need to add the var keyword to tell Dart that this is a variable 35 00:02:56,350 --> 00:03:04,670 and now we can for example use that result, the firstResult, the name is firstResult, 36 00:03:04,750 --> 00:03:07,240 we can use that here in a separate line. 37 00:03:07,240 --> 00:03:12,340 Sometimes you even just use a variable to make your code easier to read, so that you can split it across 38 00:03:12,340 --> 00:03:13,340 multiple lines, 39 00:03:13,360 --> 00:03:18,430 sometimes you have some other code in between that you need to execute first and then you want to use 40 00:03:18,430 --> 00:03:19,710 firstResult later. 41 00:03:19,810 --> 00:03:24,580 Maybe you even want to combine it with some other calculation, that you add plus one to it, 42 00:03:24,610 --> 00:03:31,390 that would be possible too. firstResult simply holds the value returned by addNumbers and that number 43 00:03:31,390 --> 00:03:32,740 in our case is a double, 44 00:03:32,740 --> 00:03:36,660 so you can do anything with it which you could do with any other double. 45 00:03:36,660 --> 00:03:45,190 So now if I run this, we print three here because firstResult has the result of addNumbers with one 46 00:03:45,190 --> 00:03:48,340 and one stored which is two and then we add one to it, 47 00:03:48,370 --> 00:03:55,240 so we have 2 + 1 and we print that result and therefore see three here. Variables are a core feature in 48 00:03:55,240 --> 00:03:58,410 Dart and we'll see it a lot throughout this course. 49 00:03:58,540 --> 00:04:06,490 Now besides that syntax where I use var for creating it, you can also create it by adding the type of data 50 00:04:06,520 --> 00:04:08,880 which this variable will hold eventually. 51 00:04:08,890 --> 00:04:12,810 So in this case, we could also write double firstResult. 52 00:04:13,090 --> 00:04:17,770 However, Dart also has a feature called type inference and 53 00:04:17,770 --> 00:04:24,400 this means it's able to infer the type of data you'll store here by the value you have on the right 54 00:04:24,400 --> 00:04:31,120 side. Here since I call addNumbers which returns a double and which always returns a double, Dart is 55 00:04:31,150 --> 00:04:34,660 able to infer that firstResult will hold a double 56 00:04:34,870 --> 00:04:37,850 and therefore you don't have to tell Dart that this is the case 57 00:04:37,930 --> 00:04:44,530 and actually it's considered a better practice to then just use var. It would be a different thing if 58 00:04:44,530 --> 00:04:49,580 you created the variable like this, without assigning an initial value. 59 00:04:49,690 --> 00:04:56,680 That is possible too, the variable now exists but it is uninitialized and sometimes this is also something 60 00:04:56,680 --> 00:05:01,120 you need to do because you want to create a variable and at a later point of time, you want to assign 61 00:05:01,120 --> 00:05:08,470 different values to it. In such a case, it is a good practice to tell Dart which kind of data will eventually 62 00:05:08,470 --> 00:05:09,880 end up in this variable, 63 00:05:09,880 --> 00:05:13,060 so then you should use the type here instead of var, 64 00:05:13,060 --> 00:05:15,420 in this case, that's the double. 65 00:05:15,440 --> 00:05:20,680 If I now run this, we see the same output as before but now we created this a bit different and we are 66 00:05:20,680 --> 00:05:22,170 using double instead of var 67 00:05:22,180 --> 00:05:31,580 here. Now that's one of the important features which I wanted to mention, another important feature is that 68 00:05:31,670 --> 00:05:36,540 Dart is a so-called object oriented programming language. 69 00:05:36,680 --> 00:05:39,440 I did mention that everything a Dart is an object, 70 00:05:39,440 --> 00:05:45,170 so this number, this one here, is an object. You would say it's a number and that's true but a number 71 00:05:45,170 --> 00:05:47,220 is just object. 72 00:05:47,220 --> 00:05:49,020 Now what is an object? 73 00:05:49,410 --> 00:05:55,080 Everything in Dart is basically a data structure with a lot of different information in it. 74 00:05:55,260 --> 00:06:02,280 Now here, the information is of course that it's one but there is some other metadata which Dart uses 75 00:06:02,280 --> 00:06:03,010 internally 76 00:06:03,030 --> 00:06:08,940 you could say. Now why do we actually start expressing our data structure as objects? 77 00:06:08,940 --> 00:06:11,670 Well, that is simply related to the real world. 78 00:06:11,700 --> 00:06:15,640 If we think about the real world, we work with objects there too, right? 79 00:06:15,690 --> 00:06:21,810 You have a car and you can drive that car or your car has a certain amount of seats or a certain amount 80 00:06:21,810 --> 00:06:22,930 of horsepower. 81 00:06:22,950 --> 00:06:29,430 So we describe our real world with objects and the idea simply is to bring that way of thinking into 82 00:06:29,460 --> 00:06:33,770 development, into programming so that it's closer to the real world. 83 00:06:33,780 --> 00:06:38,640 So we work with objects in our program because of course typically with programs, like here where we're 84 00:06:38,640 --> 00:06:43,680 building apps, we also have things which we could describe as objects, 85 00:06:43,680 --> 00:06:49,440 for example in a mobile app we have a button, a button of course would be a valid object if you think 86 00:06:49,440 --> 00:06:50,280 about it like this. 87 00:06:50,610 --> 00:06:56,720 So that's why we call it object and where this idea of using objects comes from, why we group things into 88 00:06:56,730 --> 00:06:58,930 data structures that we call objects, 89 00:06:59,040 --> 00:07:05,250 that simply comes from the real world and it should help you think about and reason about your program 90 00:07:05,640 --> 00:07:12,300 and the core idea really just is that you can kind of bring the way you think about reality into how 91 00:07:12,300 --> 00:07:14,080 you think about your programs, 92 00:07:14,100 --> 00:07:18,680 that's the rough general idea and how you work with objects will of course become very clear throughout 93 00:07:18,690 --> 00:07:22,170 this course because we work with objects all the time in there. 94 00:07:22,290 --> 00:07:27,210 Now you can also create your own objects because sometimes, you're not just working with a text or a 95 00:07:27,210 --> 00:07:32,310 number but you have a more complex data structure which you want to express in your code. 96 00:07:32,340 --> 00:07:35,330 Let's say you have a person with a name and an age. 97 00:07:35,520 --> 00:07:38,780 Of course, we could create a variable here, 98 00:07:38,850 --> 00:07:41,410 String name. String, 99 00:07:41,420 --> 00:07:44,670 this is how you tell Dart that this will be of type String. 100 00:07:44,670 --> 00:07:46,440 Please note that unlike double, 101 00:07:46,440 --> 00:07:48,180 it starts with a capital character, 102 00:07:48,180 --> 00:07:49,620 that's just how it is. 103 00:07:49,680 --> 00:07:53,610 So you could have a String name and let's say an int age, 104 00:07:53,610 --> 00:07:57,270 that would be possible and you could store values in there 105 00:07:57,690 --> 00:08:02,440 and to you as a developer it would of course be obvious that these two are related. 106 00:08:02,490 --> 00:08:05,000 However for other developers, it might not be 107 00:08:05,130 --> 00:08:11,220 and even if it is clear, it's a bit annoying that you always have to work with these two separate 108 00:08:11,250 --> 00:08:12,000 variables, 109 00:08:12,000 --> 00:08:18,030 it would be nice if you could merge it into one variable and that can be done with so-called objects. To 110 00:08:18,030 --> 00:08:23,600 create an object, you first of all need a blueprint for it that tell Dart how this 111 00:08:23,610 --> 00:08:30,390 object should look like. You do this with a core Dart feature, which you also see in other programming languages 112 00:08:30,840 --> 00:08:39,060 which is a class. A class allows you to define a blueprint for objects. A class needs to have a name 113 00:08:39,090 --> 00:08:40,430 and that could be person 114 00:08:40,500 --> 00:08:46,800 and here the naming convention is not to start with a lowercase character but with an uppercase character. 115 00:08:47,670 --> 00:08:53,130 Then you add your class body with curly braces, just as you did it for the function and still this is 116 00:08:53,130 --> 00:09:00,060 no function, this is a class, it's a different thing and in between that class, you can now define how that 117 00:09:00,240 --> 00:09:04,320 class and therefore objects based on the class should look like. 118 00:09:04,470 --> 00:09:05,610 Now what does this mean? 119 00:09:05,640 --> 00:09:13,110 For example you can add variables in here, you can add a variable name and age if you wanted to, 120 00:09:13,140 --> 00:09:19,320 so what I basically had down there is now added in that class and you can also give this default values 121 00:09:19,320 --> 00:09:20,950 just as we did it before. 122 00:09:21,000 --> 00:09:26,630 Now if you have a variable in a class, this is also sometimes called a property. 123 00:09:26,730 --> 00:09:31,010 It's still a variable, it's just inside of a class and therefore we give it a special name 124 00:09:31,050 --> 00:09:38,130 so that when I say we change this property, you know that we change a variable inside of a class. A variable 125 00:09:38,130 --> 00:09:43,140 inside of a function on the other hand is called a variable and we have these different terms so that 126 00:09:43,140 --> 00:09:47,460 when I say property, it's clear I'm referring to a variable in the class and not to a variable in the 127 00:09:47,460 --> 00:09:49,080 function. 128 00:09:49,080 --> 00:09:55,320 So here we have these two pieces of data now in our class and it is worth mentioning that just like 129 00:09:55,350 --> 00:10:00,990 with normal variables in a function, if you are assigning a value which I'm doing here with Max and 30, 130 00:10:01,290 --> 00:10:05,750 then you should actually use var here as well, instead of string and int. 131 00:10:05,790 --> 00:10:11,010 Now I use string and int here because I'll change this later to not initialize it but if you would leave it 132 00:10:11,010 --> 00:10:16,500 like this, if you plan on initializing a property with an initial value, then you should also use 133 00:10:16,530 --> 00:10:21,660 var there instead of repeating the type. Repeating the type won't be an error but it is considered a 134 00:10:21,660 --> 00:10:26,910 better practice to use var and let type inference of Dart do its job. 135 00:10:27,210 --> 00:10:35,340 And now down there in the main function, if we need such a person, we can create a variable, p1 or whatever 136 00:10:35,340 --> 00:10:42,300 you want to name it and you create a new instance of this class because the class is just a blueprint, 137 00:10:42,300 --> 00:10:49,900 the instance of the class is the concrete object with which you're working by calling person like this. 138 00:10:50,010 --> 00:10:55,140 Now if you have some other programming experience, you might be used to using the new keyword in front 139 00:10:55,140 --> 00:11:00,780 of that, new is required in some other programming languages to create a new object based on your own 140 00:11:00,780 --> 00:11:01,970 class. 141 00:11:02,010 --> 00:11:08,700 Now that can be used in Dart 2 but since Dart 2 which we use in this course, it's not required anymore 142 00:11:08,850 --> 00:11:11,640 and therefore we just call person like this. 143 00:11:11,730 --> 00:11:19,440 So now we have person executed like a function actually with parentheses here and this will instantiate 144 00:11:19,440 --> 00:11:27,210 this class and store it in p1 and therefore now if I print p1 here and I run this code, you actually 145 00:11:27,210 --> 00:11:34,610 see instance of person being printed here. Now on this class, you can now conveniently access the 146 00:11:34,610 --> 00:11:41,660 data that's stored in here with the dot notation, so I can type p1. and now I could choose name or age 147 00:11:42,350 --> 00:11:48,560 or a couple of other things which every class has in Dart, automatically added by Dart but here I 148 00:11:48,560 --> 00:11:54,460 could print p1.age and if I now run this, we see 30 here and that's what I meant. 149 00:11:54,480 --> 00:12:00,360 Now we grouped that data together and we can create new persons by simply calling person again. 150 00:12:00,360 --> 00:12:07,640 We can also change that data on a person by accessing p2.name and changing that to Manu for example 151 00:12:07,650 --> 00:12:14,580 and now if I would print p2.name down there and I print p1.name up here, so for the two different 152 00:12:14,580 --> 00:12:20,460 persons, I print the names, then you see that here on the right, we print Manu and Max because for 153 00:12:20,460 --> 00:12:25,860 the first person, I didn't change it and therefore we have that default, for the second person I did change 154 00:12:25,860 --> 00:12:31,170 it and therefore here where I changed it, we change it to Manu, down there when I print that, I print 155 00:12:31,350 --> 00:12:38,910 Manu. This is how that works and classes are a crucial feature in many programming languages and they 156 00:12:38,920 --> 00:12:45,740 also are in Dart. Now there is more about classes and about Dart which we'll learn throughout this course, 157 00:12:45,750 --> 00:12:50,100 I don't want to bore you now with a one hour long introduction to Dart, 158 00:12:50,130 --> 00:12:56,580 I find it more interesting to learn Dart whilst we are learning Flutter but these are some basics which 159 00:12:56,580 --> 00:13:02,400 I needed to get out of the way, so that we now can continue working on our main.dart file and understand 160 00:13:02,400 --> 00:13:03,780 what's going on in there. 18298

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