All language subtitles for 6. 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,190 --> 00:00:09,180 Now with our main.dart file now being totally empty, I will first of all walk through some programming 2 00:00:09,210 --> 00:00:14,850 and Dart fundamentals before we continue rebuilding our app from scratch. 3 00:00:14,850 --> 00:00:20,760 Now if you already are a more experienced developer, if you work with JavaScript or Java or C#, then 4 00:00:20,760 --> 00:00:23,900 you will already know a lot of the things I'm going to explain. 5 00:00:23,910 --> 00:00:29,160 Still, there will be some Dart specifics and therefore I would recommend that you don't skip this lecture 6 00:00:29,430 --> 00:00:33,630 but instead maybe just ramp up that playback speed if it's boring to you. 7 00:00:33,690 --> 00:00:39,570 If you're brand new to programming or you've never programmed before, then you should definitely 8 00:00:39,570 --> 00:00:41,010 follow along. 9 00:00:41,010 --> 00:00:47,610 Now for these basics, we'll not stay here in our project at the moment because we couldn't really execute that code 10 00:00:47,610 --> 00:00:50,280 here since we have no working app at the moment 11 00:00:50,370 --> 00:00:58,200 but instead you can google for DartPad and that is a web-based playground that allows you to write 12 00:00:58,200 --> 00:01:01,360 some Dart code and run it in the browser 13 00:01:01,560 --> 00:01:03,410 and that's exactly what I have here, 14 00:01:03,420 --> 00:01:09,840 this is a little demo starting project which we started with but we'll write our own code here in a 15 00:01:09,840 --> 00:01:10,770 second. 16 00:01:10,770 --> 00:01:13,010 So this is Dart code, not a Flutter app, 17 00:01:13,020 --> 00:01:16,700 just normal Dart code, you could also use Dart for web development, 18 00:01:16,770 --> 00:01:22,140 this is just some dummy Dart code where we see the output here on the right in the console and it's 19 00:01:22,140 --> 00:01:25,500 perfect for learning the basics about Dart. 20 00:01:25,710 --> 00:01:28,190 Now what do we have here? 21 00:01:28,230 --> 00:01:35,550 The overall thing we have here, this main thing, that is a so-called function. Functions are a crucial 22 00:01:35,550 --> 00:01:40,630 concept in pretty much any programming language you're going to learn, 23 00:01:40,680 --> 00:01:47,900 functions are code snippets which you can execute multiple times and at anytime you want. This function 24 00:01:47,910 --> 00:01:53,820 here is named main because the thing in front of the parentheses here is the so-called function name. 25 00:01:54,210 --> 00:02:01,160 The parentheses themselves are always required for every function you defined and they allow you to receive 26 00:02:01,220 --> 00:02:04,330 so-called arguments that would be input to your function 27 00:02:04,380 --> 00:02:06,670 and this function simply doesn't take any arguments, 28 00:02:06,690 --> 00:02:10,580 nonetheless it needs these parentheses to turn it into a function. 29 00:02:10,770 --> 00:02:12,590 Then we have curly braces, 30 00:02:12,600 --> 00:02:14,330 the green ones here right now, 31 00:02:14,430 --> 00:02:21,840 curly braces surround the so-called function body and that is the code which executes when this function 32 00:02:21,840 --> 00:02:22,800 gets called 33 00:02:22,800 --> 00:02:28,140 and I will show you how to call a function in a second. Void here, 34 00:02:28,140 --> 00:02:37,200 that is a so-called type. Dart is a typed language which simply means that everything in Dart 35 00:02:37,200 --> 00:02:38,420 has a type, 36 00:02:38,520 --> 00:02:42,310 here that is the type of data which this function returns. 37 00:02:42,330 --> 00:02:46,170 This function returns nothing and therefore the return type is void, 38 00:02:46,200 --> 00:02:52,420 it's empty so to say. Here we see a number type and this strange for thing to which I'll come back later, 39 00:02:52,530 --> 00:02:58,500 this would be the type of i, i holds a number, a so-called integer but more on that in a second, 40 00:02:58,500 --> 00:03:00,920 let's not learn too much at the same time. 41 00:03:01,080 --> 00:03:02,910 So this is a function called main, 42 00:03:02,910 --> 00:03:07,750 it has no arguments and it returns nothing and therefore has a return type of void, 43 00:03:07,770 --> 00:03:15,240 this is the function body. Main is a special function name, generally you can give your functions any 44 00:03:15,240 --> 00:03:22,860 name you want but main is a special name because main is the entry point of a Dart application. Dart 45 00:03:23,160 --> 00:03:29,310 automatically calls the main function for you when the app starts and 46 00:03:29,310 --> 00:03:35,280 therefore in a Flutter app when you add main in the main.dart file which is also a special file for 47 00:03:35,280 --> 00:03:42,090 which Flutter will look for, when you add a function called main here by adding parentheses and 48 00:03:42,090 --> 00:03:43,930 by adding void in front of it, 49 00:03:44,130 --> 00:03:50,790 this is the first function which Dart and Flutter will execute automatically when your app launches. 50 00:03:50,970 --> 00:03:52,710 So into this function, 51 00:03:52,710 --> 00:04:00,000 you need to put the code that basically kicks off that UI rendering work that brings something onto 52 00:04:00,000 --> 00:04:00,950 the screen, 53 00:04:00,960 --> 00:04:02,360 more on that in a second. 54 00:04:02,490 --> 00:04:05,340 So that's a function, that's a function 55 00:04:05,340 --> 00:04:11,040 and main is a special kind of function. Now because it's a special kind of function, you probably also 56 00:04:11,040 --> 00:04:12,970 can create other functions, right 57 00:04:13,290 --> 00:04:20,220 and you can. Let me first of all remove that code inside of the function and let's add a new function 58 00:04:20,340 --> 00:04:28,530 above the main function and now I'll name this add or addNumbers, that name is now totally up to you but 59 00:04:28,530 --> 00:04:35,460 you should follow a naming convention which is called camel case where you have only one word, so no 60 00:04:35,460 --> 00:04:36,530 blank in between, 61 00:04:36,540 --> 00:04:39,990 that would actually be a syntax error but you have one word 62 00:04:39,990 --> 00:04:47,970 and if your word actually consists of multiple words, like this one add and numbers, then all words should 63 00:04:48,000 --> 00:04:53,220 always start with a capital character even right inside of the word 64 00:04:53,220 --> 00:04:58,860 but the first word always starts with a lowercase characters. So you have addNumbers, 65 00:04:58,860 --> 00:05:04,140 if this would just be called add, then you would also start with a lowercase character but any other 66 00:05:04,140 --> 00:05:09,330 word in there has an uppercase character, that's just a naming convention which we use in Dart and 67 00:05:09,330 --> 00:05:15,540 also in many other programming languages which you should memorize and use, so that when other developers see 68 00:05:15,540 --> 00:05:22,270 your code, they have an easier time understanding it. Now as I mentioned, a function can take arguments, 69 00:05:22,360 --> 00:05:23,640 so an input. 70 00:05:23,650 --> 00:05:29,290 Now the main function didn't take any arguments but let's say addNumbers is a function which should 71 00:05:29,290 --> 00:05:35,700 hold some code to add two numbers and therefore this will now take two numbers as an input. 72 00:05:35,770 --> 00:05:41,670 So between the parentheses here, we can add num1 and num2 and just as the function name, 73 00:05:41,680 --> 00:05:43,720 these names are also up to you, 74 00:05:43,720 --> 00:05:48,880 you can name that whatever you want and as you see here, you simply add commas as a separator between 75 00:05:48,880 --> 00:05:52,380 your different arguments in that list of arguments here. 76 00:05:52,390 --> 00:05:57,470 Now you also need a function body and you add that by using curly braces, 77 00:05:57,580 --> 00:06:04,120 that simply tells Dart that in between the curly braces, you have the code that should execute when this 78 00:06:04,120 --> 00:06:06,130 function gets called, 79 00:06:06,130 --> 00:06:11,080 so when you instruct it to execute the code inside of addNumbers. 80 00:06:11,590 --> 00:06:17,350 Now you do instruct Dart to execute this function by using its name, 81 00:06:17,380 --> 00:06:21,760 now in the main function which is the code that first runs when the app starts. 82 00:06:21,760 --> 00:06:27,010 So if here, in main, you simply call addNumbers by its name 83 00:06:27,010 --> 00:06:30,130 and now here you also need to add parentheses. 84 00:06:30,130 --> 00:06:33,900 You always need to do this, even if the function takes no arguments, 85 00:06:33,910 --> 00:06:41,050 this function however does take arguments so now between these parentheses, you add the concrete values 86 00:06:41,140 --> 00:06:43,460 which will be passed to addNumbers. 87 00:06:43,600 --> 00:06:49,460 So the num1 and num2 do have actual data with which they can do their work, 88 00:06:49,510 --> 00:06:52,020 so here we could add 1 and 2. 89 00:06:52,300 --> 00:06:55,110 Now of course this function doesn't do anything at the moment though, 90 00:06:55,270 --> 00:06:57,240 I call it with 1 and 2 here 91 00:06:57,280 --> 00:07:01,140 but we have no logic in there to really add these numbers. 92 00:07:01,930 --> 00:07:07,720 So what we can do in here is we can call num1 + num2 and now we would add that. 93 00:07:07,850 --> 00:07:13,630 Now we also need to add a semicolon, that's another thing in Dart, for every expression you have in your 94 00:07:13,630 --> 00:07:20,730 code, for every code line, you have to add a semicolon in the end, exceptions from the rule are the definition of 95 00:07:20,740 --> 00:07:26,650 functions where you don't add a semicolon after this curly brace here but you do add it here for all 96 00:07:26,650 --> 00:07:28,310 the code inside of your functions 97 00:07:28,360 --> 00:07:33,130 so to say. And in general, for all these block statements as they are called, 98 00:07:33,210 --> 00:07:38,230 so these statements where you have code between curly braces, you don't add a semicolon after the curly 99 00:07:38,230 --> 00:07:42,560 braces, you only add semicolons after the expressions between curly braces. 100 00:07:42,580 --> 00:07:45,850 So here we have num1 + num2, I added the semicolon here 101 00:07:45,850 --> 00:07:49,460 but the problem we now have is we add two numbers here 102 00:07:49,570 --> 00:07:56,340 and that would generally work but we're not doing anything with the result of this operation. 103 00:07:56,380 --> 00:08:00,780 Now you could think that it should automatically be output here on the right, 104 00:08:00,790 --> 00:08:01,770 well let's see. 105 00:08:01,870 --> 00:08:03,780 Let's click run, 106 00:08:03,950 --> 00:08:07,200 what happens is that we have an empty output here on the right. 107 00:08:07,200 --> 00:08:07,830 Huh, 108 00:08:07,880 --> 00:08:09,610 so that is not really helpful. 109 00:08:09,920 --> 00:08:14,600 Now in programming, you always have to be very clear about what should happen because the programming 110 00:08:14,600 --> 00:08:18,500 language, Dart in this case, does not make any assumptions. 111 00:08:18,530 --> 00:08:20,130 Why would it print it here, 112 00:08:20,150 --> 00:08:23,940 you don't tell it to do that and therefore it doesn't do it. 113 00:08:23,960 --> 00:08:32,090 Now you can print the result by using the print function, print is not a function we defined, it's a function 114 00:08:32,090 --> 00:08:34,670 that's built into Dart 115 00:08:35,210 --> 00:08:35,950 and you can wrap 116 00:08:35,950 --> 00:08:38,000 num1 + num2 with print 117 00:08:38,150 --> 00:08:42,380 and if you now hit run, you will see three here on the right, 118 00:08:42,380 --> 00:08:51,780 so now this works and now we're outputting the result of num1 + num2. So this is our first little piece 119 00:08:51,780 --> 00:08:55,650 of Dart code and yet we can improve that, 120 00:08:55,650 --> 00:09:02,360 for example we don't use types here but Dart is a strongly and a strictly typed programming language. 121 00:09:02,460 --> 00:09:10,890 That means that you have to be clear about which type of data your arguments are or this function returns, 122 00:09:11,100 --> 00:09:17,760 that is something which helps the Dart compiler to yell at you when you write code that violates that. If 123 00:09:17,760 --> 00:09:22,740 you don't assign your own types which will do in the second, then Dart will assume that this is of the 124 00:09:22,860 --> 00:09:28,720 dynamic type, a type built into Dart which as the name suggests doesn't tell too much about the type, 125 00:09:28,770 --> 00:09:33,930 it basically accepts any value and therefore it won't really help you that much with checking that you're 126 00:09:33,930 --> 00:09:35,060 writing proper code. 127 00:09:35,310 --> 00:09:43,230 So if possible, you should avoid that dynamic type and assign explicit types when working with Dart. Now 128 00:09:43,240 --> 00:09:46,390 there are a couple of built-in types in Dart. 129 00:09:46,390 --> 00:09:54,640 Everything in general is a so-called object, which is a data structure that has some complex 130 00:09:54,640 --> 00:09:55,780 logic inside of it, 131 00:09:55,810 --> 00:09:59,780 possibly at least and we'll create such objects later too 132 00:09:59,800 --> 00:10:07,210 but there are certain special types of objects you could say. There is text, for example and you create 133 00:10:07,240 --> 00:10:10,510 text by using quotation marks. 134 00:10:10,510 --> 00:10:17,560 So here if I type hello and I print this, please note the quotes, you can use single or double quotes, 135 00:10:17,560 --> 00:10:19,760 that doesn't matter but you shouldn't mix them, 136 00:10:19,780 --> 00:10:25,150 so if you open it with a double quote, you also have to close it with a double quote. I will use single 137 00:10:25,150 --> 00:10:29,830 quotes in this course though and this creates a so-called string data type, 138 00:10:29,830 --> 00:10:35,210 so this is a value which is of type string, text simply is a string. 139 00:10:35,420 --> 00:10:39,830 If we now hit run, you will see hello being printed because 140 00:10:39,910 --> 00:10:46,980 I'm printing this here and therefore we see the output here. So text strings are one data type. 141 00:10:47,090 --> 00:10:54,830 Besides the text, for our numbers we have so-called integers and we have floats or doubles. Integers are 142 00:10:54,830 --> 00:11:04,000 numbers without any decimal places, like 29 or 43 or -10. Floats or doubles are 143 00:11:04,000 --> 00:11:13,950 numbers with decimal places like 29.99, -10.56, something like that. So 144 00:11:13,950 --> 00:11:18,470 here, these two numbers would actually be integers. 145 00:11:18,570 --> 00:11:25,350 Now in this function here, we should be clear about the fact that this only works with integers and 146 00:11:25,380 --> 00:11:29,760 we do that by adding int in front of each argument. 147 00:11:29,760 --> 00:11:35,670 This tells Dart that num1 and num2 both have to be integers. 148 00:11:35,670 --> 00:11:38,340 So if I now run this, it will still work 149 00:11:38,340 --> 00:11:45,330 but if I would pass 2.5 or 2.6 here, then we actually automatically get an error here in the bottom 150 00:11:45,330 --> 00:11:46,300 right corner 151 00:11:46,320 --> 00:11:52,560 that an argument of type double can't be assigned to a parameter, parameter is basically a different word 152 00:11:52,560 --> 00:11:55,320 for argument, of type int 153 00:11:55,500 --> 00:12:03,570 and that is why Dart does use these types, because it allows us to write cleaner and better code. If we 154 00:12:03,570 --> 00:12:09,750 know that this function should only work with integers, then we should define it as such and if we then 155 00:12:09,750 --> 00:12:18,000 later somewhere else in our code accidentally call that with a double, we get an error and we get that 156 00:12:18,060 --> 00:12:22,070 error before we ship our code into the Flutter app, 157 00:12:22,080 --> 00:12:27,960 so into the app stores for example and that is of course good, we want to catch and fix errors 158 00:12:28,170 --> 00:12:30,140 whilst we are developing. 159 00:12:30,270 --> 00:12:32,690 So here, we would get an error. 160 00:12:32,700 --> 00:12:34,330 Now we have two possible fixes, 161 00:12:34,380 --> 00:12:40,820 either we fix our code such that we only call that with an integer value or if we see no no no, 162 00:12:40,830 --> 00:12:43,550 this should actually also work with doubles, 163 00:12:43,560 --> 00:12:49,430 well in such a case, we could of course change that and accept doubles as inputs 164 00:12:49,680 --> 00:12:56,190 and now if we run this, this code would work again and it would output a double here with a decimal place 165 00:12:56,400 --> 00:12:58,950 because now we allow doubles here. 166 00:12:58,950 --> 00:13:03,510 Now of course you could think, why don't we always use a double then, it's more flexible than an int? 167 00:13:03,540 --> 00:13:08,350 Well sometimes you have situations where you really only want to have numbers without decimal places, 168 00:13:08,370 --> 00:13:12,530 let's say you have some counter, which only increment by one at a time, 169 00:13:12,570 --> 00:13:17,650 well then you should be clear about that being an int so that you can't accidentally run the code with 170 00:13:17,650 --> 00:13:20,700 an invalid value with a decimal value. 171 00:13:20,700 --> 00:13:27,720 Now side note, we could have also used num here which is the overarching number type and that would allow 172 00:13:27,720 --> 00:13:31,020 both ints and doubles but as you see, if we use double here, 173 00:13:33,790 --> 00:13:40,090 this also allows us to call this with a 1 because this implicitly is converted to a double. 174 00:13:40,090 --> 00:13:46,800 So we can really use double here if we want to allow both double and int. So this works 175 00:13:46,810 --> 00:13:53,350 and with that, we learned about text, so-called strings and about numbers where we have integers and doubles 176 00:13:53,800 --> 00:14:02,500 and that all can be used as types which we for example pass to a function. Now this function however 177 00:14:02,860 --> 00:14:06,700 might also return something or it might not 178 00:14:06,700 --> 00:14:12,330 and you should therefore also always add a type information about what type of data 179 00:14:12,340 --> 00:14:13,720 this function returns. 180 00:14:13,730 --> 00:14:19,570 Now this function prints the result and therefore, it does not return anything to the place where we call 181 00:14:19,570 --> 00:14:20,170 it, 182 00:14:20,170 --> 00:14:22,470 hence the correct type here would be void. 183 00:14:22,510 --> 00:14:24,250 Void is a special type in Dart 184 00:14:24,250 --> 00:14:26,110 which basically means nothing, 185 00:14:26,170 --> 00:14:28,540 this function returns nothing. 186 00:14:28,540 --> 00:14:30,080 Now why is that important? 187 00:14:30,130 --> 00:14:40,160 Because if we now for example wanted to print the result of addNumbers here, like this, then we get an 188 00:14:40,160 --> 00:14:46,010 error that the expression here has a type of void and therefore can't be printed, we can't print the 189 00:14:46,010 --> 00:14:50,420 result of us calling addNumbers because addNumbers doesn't return anything. 190 00:14:50,420 --> 00:14:54,210 Sure, it prints something but that is done from inside the function, 191 00:14:54,320 --> 00:15:00,170 here we would want to print what this function gives back and this function doesn't give us 192 00:15:00,230 --> 00:15:01,200 anything back. 193 00:15:01,220 --> 00:15:05,720 However, we could actually make sure that it does. Instead of printing in here, 194 00:15:05,720 --> 00:15:09,770 let me comment this out what you can do by adding two forward slashes, 195 00:15:09,810 --> 00:15:15,860 now this code won't be considered and this is a common pattern by the way, that you comment out code 196 00:15:15,890 --> 00:15:18,410 which you don't want to run but maybe you want to use it later, 197 00:15:18,410 --> 00:15:22,970 so you don't want to delete it but you simply comment it out so that it's still there but not 198 00:15:22,970 --> 00:15:23,720 executed 199 00:15:23,900 --> 00:15:30,620 and now we could return something here by using a special keyword in Dart, which is return. Now this is a 200 00:15:30,620 --> 00:15:36,380 keyword which tells Dart that this will now return something to the place where you called the function. 201 00:15:37,120 --> 00:15:39,920 So here, I return num1 + num2 202 00:15:39,950 --> 00:15:41,320 and now what this does is 203 00:15:41,450 --> 00:15:47,480 now here, this is valid code because now here we print the result of addNumbers and the result is what 204 00:15:47,480 --> 00:15:48,820 addNumbers returns 205 00:15:48,860 --> 00:15:50,950 and here we return two numbers. 206 00:15:50,950 --> 00:15:57,300 If I now run this, we get an error though that we can't return a value because this expression has a 207 00:15:57,290 --> 00:15:59,030 return type of void 208 00:15:59,090 --> 00:16:00,810 and again, this is a good thing. 209 00:16:00,970 --> 00:16:06,900 We defined that addNumbers doesn't return anything, that it returns nothing 210 00:16:07,040 --> 00:16:08,970 and yet we do return something here. 211 00:16:09,020 --> 00:16:12,670 Now the solution is to change the return type here 212 00:16:12,860 --> 00:16:20,240 and here, we can change this to double because since we add two doubles, we will always return a double. 213 00:16:20,240 --> 00:16:25,370 So now I'm returning a double here and now if I call this function and I run this, 214 00:16:25,370 --> 00:16:29,500 now we don't get an error and we see 3.6 here instead. 215 00:16:29,580 --> 00:16:32,810 This is how you should write Dart code, you should use these types, 216 00:16:32,930 --> 00:16:38,720 you have the core types of string and numbers where we have integers and doubles with a couple of other 217 00:16:38,720 --> 00:16:41,190 types too, which we'll learn about throughout this course 218 00:16:41,390 --> 00:16:48,860 and this is a core concept of Dart, just as functions are a core concept of Dart because now we can call 219 00:16:49,310 --> 00:16:51,280 addNumbers here as often as we want. 220 00:16:51,350 --> 00:16:56,520 So I can also call addNumbers here with two 1s for example and 221 00:16:56,660 --> 00:17:02,420 this is a really useful pattern and feature which you have in basically any programming language because 222 00:17:02,420 --> 00:17:04,460 it allows you to write code once, 223 00:17:04,550 --> 00:17:08,870 this logic for adding code and you can call it with different inputs 224 00:17:08,870 --> 00:17:14,900 as often as you want and therefore, functions are a really important concept in programming and also 225 00:17:14,990 --> 00:17:15,530 in Dart. 24141

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