All language subtitles for 8. [Dart] Data Types

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: 0 1 00:00:00,240 --> 00:00:06,960 Now in the last lesson, we saw inside dartpad when we tried to change the variable that's holding a 1 2 00:00:06,960 --> 00:00:11,200 string namely a string of characters, so basically text. 2 3 00:00:11,370 --> 00:00:15,340 And we tried to make it hold a number instead, or an integer. 3 4 00:00:15,360 --> 00:00:18,920 It wasn't really happy with that and it didn't want to do it for me. 4 5 00:00:19,170 --> 00:00:24,210 In order to understand what's going on, we have to understand Dart data types. 5 6 00:00:24,210 --> 00:00:28,850 Dart is what we call a statically typed language. 6 7 00:00:28,890 --> 00:00:29,790 What does that mean? 7 8 00:00:30,030 --> 00:00:37,080 Well let's say we create a variable called myVar. It kind of acts a bit like one of those children's 8 9 00:00:37,080 --> 00:00:42,840 toys which has different shaped holes and you have to fit the right shape inside the right hole. 9 10 00:00:42,840 --> 00:00:48,600 So for example, if I had something that was circular, then that would go through the circular hole. 10 11 00:00:48,750 --> 00:00:55,110 But if I had something that was a triangle for example, then it won't go through the hole. And it's very 11 12 00:00:55,110 --> 00:00:55,980 similar 12 13 00:00:55,980 --> 00:01:03,270 when we have statically typed languages like Dart. Essentially when I create a variable and I say that 13 14 00:01:03,270 --> 00:01:06,650 this variable can only hold pieces of text, 14 15 00:01:07,110 --> 00:01:11,910 then if I try to put a number into this variable it won't accept it. 15 16 00:01:12,030 --> 00:01:15,030 It says I can't mix up my types. 16 17 00:01:15,210 --> 00:01:17,880 What are these types that we're talking about? 17 18 00:01:17,880 --> 00:01:20,460 Well they're essentially types of data. 18 19 00:01:20,460 --> 00:01:27,450 So we've got strings, which are essentially a string of characters like a string of pearls, but we've 19 20 00:01:27,450 --> 00:01:30,390 strung together some characters instead. 20 21 00:01:30,390 --> 00:01:38,700 And in Dart, its convention to have a single quote around the piece of text to represent a string. 21 22 00:01:38,700 --> 00:01:44,820 So we know that if we don't have single quotes, the computer will think that we're writing code. But if 22 23 00:01:44,820 --> 00:01:50,580 we do have the single quotes, then it'll know that we're trying to specify a string and it doesn't treat 23 24 00:01:50,580 --> 00:01:52,250 it as code. 24 25 00:01:52,290 --> 00:02:00,660 Now there's also int, which is short for integer. And integers are basically just whole numbers. 25 26 00:02:00,690 --> 00:02:04,930 They can be positive or negative, but they can't have a decimal point. 26 27 00:02:05,010 --> 00:02:10,590 So if you wanted a decimal point in Dart, you would be working with something called a double. 27 28 00:02:10,590 --> 00:02:13,850 And this is a data type that allows you to have a decimal point 28 29 00:02:14,280 --> 00:02:18,810 and it will hold your data with all those numbers after the decimal point. 29 30 00:02:18,810 --> 00:02:24,450 Now the last data type that you'll see commonly occurring, is something called a bool which is short 30 31 00:02:24,450 --> 00:02:32,130 for a boolean. And it's a data type that can only hold one of two values, either true or false. 31 32 00:02:32,130 --> 00:02:37,150 And all of these data types together are known as primitive data types. 32 33 00:02:37,150 --> 00:02:42,750 So they're the most basic fundamental building blocks of any computer program. 33 34 00:02:42,750 --> 00:02:49,020 And we're going to use them as we build up more and more complex apps to store more and more complex 34 35 00:02:49,020 --> 00:02:55,010 data. Coming back to this idea of Dart being a statically typed language. 35 36 00:02:55,080 --> 00:03:02,940 It means that at the point when we create the variable, it will get a data type and at a later date, we 36 37 00:03:02,940 --> 00:03:10,360 can change what's stored inside the variable as long as we don't change the data type. Heading back into 37 38 00:03:10,360 --> 00:03:11,470 our dartpad 38 39 00:03:11,470 --> 00:03:18,400 let's delete everything inside our main function. And this time let's create some other variables. 39 40 00:03:18,520 --> 00:03:23,800 We're going to create a variable a, and we're going to assign this to the value 40 41 00:03:23,800 --> 00:03:25,820 hello. 41 42 00:03:25,820 --> 00:03:32,290 Now at the moment when I double click on this a variable ,the dartpad very helpfully points out that 42 43 00:03:32,290 --> 00:03:34,640 this is a string. 43 44 00:03:34,750 --> 00:03:39,220 This variable holds data that has to be of the type string. 44 45 00:03:39,370 --> 00:03:39,690 So 45 46 00:03:39,880 --> 00:03:40,360 hello 46 47 00:03:40,360 --> 00:03:42,460 works or bye 47 48 00:03:42,550 --> 00:03:47,730 or anything that's inside a set of single quotes will work just fine. 48 49 00:03:48,400 --> 00:03:52,130 But I can't change the data type of this variable. 49 50 00:03:52,150 --> 00:03:55,290 I can't for example change it to 123. 50 51 00:03:55,330 --> 00:04:01,120 Now this is what we mean when we say that Dart is a statically typed language. 51 52 00:04:01,120 --> 00:04:09,130 If it was a dynamically type language and there's many of those out there, then this would be a perfectly 52 53 00:04:09,130 --> 00:04:10,720 valid thing to do. 53 54 00:04:10,720 --> 00:04:12,450 And it won't complain at all. 54 55 00:04:12,490 --> 00:04:16,280 It'll just silently suck it up and probably judge you, 55 56 00:04:16,300 --> 00:04:19,980 the human whose programming this, for being so indecisive. 56 57 00:04:20,140 --> 00:04:22,960 But it won't actually have a problem. 57 58 00:04:22,990 --> 00:04:25,600 And one of those languages is JavaScript. 58 59 00:04:25,600 --> 00:04:30,760 So if I straight up just take these two lines of code that we've already seen leads to all sorts of 59 60 00:04:30,760 --> 00:04:38,260 problems in Dart, and I put it inside a JavaScript playground such as jsbin, and if I console log, which 60 61 00:04:38,260 --> 00:04:46,150 is the JavaScript equivalent of print, the value of the variable a, then it'll happily print 123 61 62 00:04:46,150 --> 00:04:46,840 in here. 62 63 00:04:47,080 --> 00:04:50,920 And if I put it here, then it'll print hello. 63 64 00:04:50,950 --> 00:04:57,970 So you can see that these are completely different data types and being able to change the data type 64 65 00:04:58,180 --> 00:05:05,170 that a variable holds, makes JavaScript a dynamically typed programming language. 65 66 00:05:05,190 --> 00:05:12,330 Now there are pros and cons of each type of programming language. Dynamically typed languages are more 66 67 00:05:12,330 --> 00:05:13,010 flexible. 67 68 00:05:13,020 --> 00:05:16,280 You can change things on the fly, where statically typed 68 69 00:05:16,290 --> 00:05:22,200 programming languages are generally safer. You're less likely to run into problems because you forgot 69 70 00:05:22,200 --> 00:05:26,790 that, 'oh a was actually meant to be a variable that only holds strings'. 70 71 00:05:26,850 --> 00:05:29,550 It'll actually enforce it for you. 71 72 00:05:29,550 --> 00:05:37,530 Now with Dart though, there is a way of turning it into a dynamic programming language. And it might actually 72 73 00:05:37,530 --> 00:05:41,190 trip you up at some point. here in this line. 73 74 00:05:41,190 --> 00:05:44,880 We've created our variable a and we've set it to equal hello. 74 75 00:05:45,240 --> 00:05:54,120 But the reason why 'a' knows that it should store a string is because it's inferring the data type from 75 76 00:05:54,120 --> 00:05:59,540 the value. Because it knows that the value that you've given it, is a string. 76 77 00:05:59,550 --> 00:06:03,530 So therefore I must be a variable that holds strings. 77 78 00:06:03,600 --> 00:06:11,160 And similarly if I changed 'a' to hold a number, then it will be able to correctly infer that 'oh I'm actually 78 79 00:06:11,160 --> 00:06:17,520 a variable that holds integers' And it's very clever about this because as soon as you change the value 79 80 00:06:17,820 --> 00:06:25,050 to have a decimal point, then it knows 'oh actually now I'm a variable that can hold doubles.' 80 81 00:06:25,260 --> 00:06:27,330 But what if you did this. 81 82 00:06:27,870 --> 00:06:32,460 What if you create this variable and you don't give it a value at all? 82 83 00:06:32,670 --> 00:06:35,410 You just say here's an empty box. 83 84 00:06:35,460 --> 00:06:36,980 It's called a''. 84 85 00:06:36,980 --> 00:06:43,920 And at some point later on down the line, you decide to give it a value let's say, 'a' equals 12 85 86 00:06:43,920 --> 00:06:45,080 3. 86 87 00:06:45,090 --> 00:06:49,830 Now what do we think is the data type of 'a' at this point? 87 88 00:06:49,830 --> 00:07:02,730 Now at this point, 'a' is now dynamic. And I can prove that because I can now set 'a' to equal a string and 88 89 00:07:02,880 --> 00:07:05,730 it won't complain about this at all. 89 90 00:07:05,730 --> 00:07:14,610 And in fact I can even print my string 'a' into the console and everything will work just as you would 90 91 00:07:14,610 --> 00:07:17,940 expect for a dynamically typed programming language. 91 92 00:07:18,450 --> 00:07:19,770 So what's going on here? 92 93 00:07:20,580 --> 00:07:30,990 Well, Dart also has a data type called dynamic. And what this means is that you can create a variable that 93 94 00:07:30,990 --> 00:07:34,770 doesn't have to have a fixed data type. 94 95 00:07:34,770 --> 00:07:42,120 So if I was creating a variable and I wanted it to be a fixed string data type, then I would write string. 95 96 00:07:43,080 --> 00:07:49,650 If I wanted to create a variable that had an integer as the data type, then I would write 'int' in the beginning 96 97 00:07:49,710 --> 00:07:51,710 instead of var. 97 98 00:07:51,720 --> 00:07:57,160 Now I could also write dynamic as the key word. 98 99 00:07:57,720 --> 00:08:02,700 And now my variable c is dynamic. 99 100 00:08:02,700 --> 00:08:12,090 So if 'a' is only able to hold strings, then I can put the value 'hello' inside it. But I can't put the value 100 101 00:08:12,090 --> 00:08:13,530 123. 101 102 00:08:13,560 --> 00:08:23,880 This will generate an error. But if I had my variable created as a dynamic type, then I can put any sort 102 103 00:08:23,880 --> 00:08:31,200 of data in it. I can put 123 and that will be just fine. It'll print out c without a problem 103 104 00:08:33,170 --> 00:08:37,010 I can change c into a string if I wanted to. 104 105 00:08:37,220 --> 00:08:41,060 And now when I print the string, hello 105 106 00:08:41,090 --> 00:08:43,130 now gets printed. 106 107 00:08:43,250 --> 00:08:52,850 This is how dynamic types work. And when we created the var without giving it a value, we essentially 107 108 00:08:52,850 --> 00:08:57,140 created a dynamic type variable. 108 109 00:08:57,200 --> 00:09:04,520 So when I click on 'a' you can see it's data type is dynamic. Because it didn't get a value, it couldn't 109 110 00:09:04,520 --> 00:09:08,070 work out what data type it was meant to hold. 110 111 00:09:08,180 --> 00:09:11,870 So it instead created a dynamic type. 111 112 00:09:11,900 --> 00:09:20,000 The important thing here to take away is that if you want to use Dart for its type safety, 112 113 00:09:20,000 --> 00:09:26,450 so ensuring that you don't mess up the data type and you don't accidentally put in the wrong thing that 113 114 00:09:26,450 --> 00:09:33,750 you didn't mean to, then it's best to create your variables with the data type to start with. 114 115 00:09:34,130 --> 00:09:41,630 So if we wanted to create a variable that holds a string, then we would write string a = hello or 115 116 00:09:41,690 --> 00:09:43,220 whatever value you want. 116 117 00:09:43,490 --> 00:09:50,330 If we wanted to create a variable that's an int, then we would write int b = 123 or whatever 117 118 00:09:50,330 --> 00:09:51,350 maybe. 118 119 00:09:51,440 --> 00:09:58,280 And if you're creating a variable that you don't yet have a value for, say the score of the game as soon 119 120 00:09:58,340 --> 00:10:01,200 as the game starts, won't really have a value. 120 121 00:10:01,310 --> 00:10:08,300 Well then in that case you can create an int and give it a name, but not give it a value. 121 122 00:10:08,720 --> 00:10:14,540 But this way further down the line, c can never pulled a string. 122 123 00:10:14,570 --> 00:10:20,750 It can't ever change its data type because when we created it, we already specified it. 123 124 00:10:21,320 --> 00:10:31,490 So in terms of best practice, I recommend to avoid using var, and avoid using dynamic data types unless 124 125 00:10:31,580 --> 00:10:38,630 you actually need it for a reason. Because it can lead to unexpected things happening in your code, and 125 126 00:10:38,630 --> 00:10:41,920 it can lead to crashes down the line. 126 127 00:10:41,930 --> 00:10:48,630 So in this lesson, we talked about data types and the primitive data types that Dart has access to. 127 128 00:10:49,250 --> 00:10:55,280 And we also talked about how Dart can be a statically typed language because once you create a variable 128 129 00:10:55,310 --> 00:11:00,890 that has a fixed type, then it won't accept values that are of different data types. 129 130 00:11:01,400 --> 00:11:09,230 But we also saw how Dart can have this dynamic typing, which makes it a lot more similar to dynamically 130 131 00:11:09,230 --> 00:11:16,740 type languages such as JavaScript. Now the official Dart documentation says that Dart 2, 131 132 00:11:16,760 --> 00:11:23,930 so the latest version of Dart, is a statically typed language. Which means that we can't switch around 132 133 00:11:23,930 --> 00:11:32,090 the data type of a variable once we've created it. But it does say that you can use this dynamic keyword 133 134 00:11:32,570 --> 00:11:36,200 if you want to be able to change the data type. 134 135 00:11:36,200 --> 00:11:44,630 But it does of course remove the benefits of a type-safe language. So I recommend to avoid using the 135 136 00:11:44,630 --> 00:11:51,860 keyword var, and avoid using dynamic types when you're working with Dart, in order for your code to be less 136 137 00:11:52,010 --> 00:11:53,300 error prone. 137 138 00:11:53,310 --> 00:11:59,600 Now at this stage, it might not affect us very much. But later, on as our code gets more complex and does 138 139 00:11:59,600 --> 00:12:05,510 a lot more things, it becomes a lot easier to work with Dart as a statically type language. 139 140 00:12:05,510 --> 00:12:11,780 But if you come from JavaScript or another language that has dynamic typing, just be aware that you can 140 141 00:12:11,780 --> 00:12:16,590 also do the same with Dart. Now in the next lesson, 141 142 00:12:16,610 --> 00:12:22,610 we're going to head back to making our apps and we're going to learn about stateful and stateless 142 143 00:12:22,610 --> 00:12:23,750 widgets in Flutter. 15948

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