All language subtitles for 8. [Dart] Data Types

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
fr French Download
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

0 Now in the last lesson, we saw inside dartpad when we tried to change the variable that's holding a 1

string namely a string of characters, so basically text. 2

And we tried to make it hold a number instead, or an integer. 3

It wasn't really happy with that and it didn't want to do it for me. 4

In order to understand what's going on, we have to understand Dart data types. 5

Dart is what we call a statically typed language. 6

What does that mean? 7

Well let's say we create a variable called myVar. It kind of acts a bit like one of those children's 8

toys which has different shaped holes and you have to fit the right shape inside the right hole. 9

So for example, if I had something that was circular, then that would go through the circular hole. 10

But if I had something that was a triangle for example, then it won't go through the hole. And it's very 11

similar 12

when we have statically typed languages like Dart. Essentially when I create a variable and I say that 13

this variable can only hold pieces of text, 14

then if I try to put a number into this variable it won't accept it. 15

It says I can't mix up my types. 16

What are these types that we're talking about? 17

Well they're essentially types of data. 18

So we've got strings, which are essentially a string of characters like a string of pearls, but we've 19

strung together some characters instead. 20

And in Dart, its convention to have a single quote around the piece of text to represent a string. 21

So we know that if we don't have single quotes, the computer will think that we're writing code. But if 22

we do have the single quotes, then it'll know that we're trying to specify a string and it doesn't treat 23

it as code. 24

Now there's also int, which is short for integer. And integers are basically just whole numbers. 25

They can be positive or negative, but they can't have a decimal point. 26

So if you wanted a decimal point in Dart, you would be working with something called a double. 27

And this is a data type that allows you to have a decimal point 28

and it will hold your data with all those numbers after the decimal point. 29

Now the last data type that you'll see commonly occurring, is something called a bool which is short 30

for a boolean. And it's a data type that can only hold one of two values, either true or false. 31

And all of these data types together are known as primitive data types. 32

So they're the most basic fundamental building blocks of any computer program. 33

And we're going to use them as we build up more and more complex apps to store more and more complex 34

data. Coming back to this idea of Dart being a statically typed language. 35

It means that at the point when we create the variable, it will get a data type and at a later date, we 36

can change what's stored inside the variable as long as we don't change the data type. Heading back into 37

our dartpad 38

let's delete everything inside our main function. And this time let's create some other variables. 39

We're going to create a variable a, and we're going to assign this to the value 40

hello. 41

Now at the moment when I double click on this a variable ,the dartpad very helpfully points out that 42

this is a string. 43

This variable holds data that has to be of the type string. 44

So 45

hello 46

works or bye 47

or anything that's inside a set of single quotes will work just fine. 48

But I can't change the data type of this variable. 49

I can't for example change it to 123. 50

Now this is what we mean when we say that Dart is a statically typed language. 51

If it was a dynamically type language and there's many of those out there, then this would be a perfectly 52

valid thing to do. 53

And it won't complain at all. 54

It'll just silently suck it up and probably judge you, 55

the human whose programming this, for being so indecisive. 56

But it won't actually have a problem. 57

And one of those languages is JavaScript. 58

So if I straight up just take these two lines of code that we've already seen leads to all sorts of 59

problems in Dart, and I put it inside a JavaScript playground such as jsbin, and if I console log, which 60

is the JavaScript equivalent of print, the value of the variable a, then it'll happily print 123 61

in here. 62

And if I put it here, then it'll print hello. 63

So you can see that these are completely different data types and being able to change the data type 64

that a variable holds, makes JavaScript a dynamically typed programming language. 65

Now there are pros and cons of each type of programming language. Dynamically typed languages are more 66

flexible. 67

You can change things on the fly, where statically typed 68

programming languages are generally safer. You're less likely to run into problems because you forgot 69

that, 'oh a was actually meant to be a variable that only holds strings'. 70

It'll actually enforce it for you. 71

Now with Dart though, there is a way of turning it into a dynamic programming language. And it might actually 72

trip you up at some point. here in this line. 73

We've created our variable a and we've set it to equal hello. 74

But the reason why 'a' knows that it should store a string is because it's inferring the data type from 75

the value. Because it knows that the value that you've given it, is a string. 76

So therefore I must be a variable that holds strings. 77

And similarly if I changed 'a' to hold a number, then it will be able to correctly infer that 'oh I'm actually 78

a variable that holds integers' And it's very clever about this because as soon as you change the value 79

to have a decimal point, then it knows 'oh actually now I'm a variable that can hold doubles.' 80

But what if you did this. 81

What if you create this variable and you don't give it a value at all? 82

You just say here's an empty box. 83

It's called a''. 84

And at some point later on down the line, you decide to give it a value let's say, 'a' equals 12 85

3. 86

Now what do we think is the data type of 'a' at this point? 87

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

it won't complain about this at all. 89

And in fact I can even print my string 'a' into the console and everything will work just as you would 90

expect for a dynamically typed programming language. 91

So what's going on here? 92

Well, Dart also has a data type called dynamic. And what this means is that you can create a variable that 93

doesn't have to have a fixed data type. 94

So if I was creating a variable and I wanted it to be a fixed string data type, then I would write string. 95

If I wanted to create a variable that had an integer as the data type, then I would write 'int' in the beginning 96

instead of var. 97

Now I could also write dynamic as the key word. 98

And now my variable c is dynamic. 99

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

123. 101

This will generate an error. But if I had my variable created as a dynamic type, then I can put any sort 102

of data in it. I can put 123 and that will be just fine. It'll print out c without a problem 103

I can change c into a string if I wanted to. 104

And now when I print the string, hello 105

now gets printed. 106

This is how dynamic types work. And when we created the var without giving it a value, we essentially 107

created a dynamic type variable. 108

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

work out what data type it was meant to hold. 110

So it instead created a dynamic type. 111

The important thing here to take away is that if you want to use Dart for its type safety, 112

so ensuring that you don't mess up the data type and you don't accidentally put in the wrong thing that 113

you didn't mean to, then it's best to create your variables with the data type to start with. 114

So if we wanted to create a variable that holds a string, then we would write string a = hello or 115

whatever value you want. 116

If we wanted to create a variable that's an int, then we would write int b = 123 or whatever 117

maybe. 118

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

as the game starts, won't really have a value. 120

Well then in that case you can create an int and give it a name, but not give it a value. 121

But this way further down the line, c can never pulled a string. 122

It can't ever change its data type because when we created it, we already specified it. 123

So in terms of best practice, I recommend to avoid using var, and avoid using dynamic data types unless 124

you actually need it for a reason. Because it can lead to unexpected things happening in your code, and 125

it can lead to crashes down the line. 126

So in this lesson, we talked about data types and the primitive data types that Dart has access to. 127

And we also talked about how Dart can be a statically typed language because once you create a variable 128

that has a fixed type, then it won't accept values that are of different data types. 129

But we also saw how Dart can have this dynamic typing, which makes it a lot more similar to dynamically 130

type languages such as JavaScript. Now the official Dart documentation says that Dart 2, 131

so the latest version of Dart, is a statically typed language. Which means that we can't switch around 132

the data type of a variable once we've created it. But it does say that you can use this dynamic keyword 133

if you want to be able to change the data type. 134

But it does of course remove the benefits of a type-safe language. So I recommend to avoid using the 135

keyword var, and avoid using dynamic types when you're working with Dart, in order for your code to be less 136

error prone. 137

Now at this stage, it might not affect us very much. But later, on as our code gets more complex and does 138

a lot more things, it becomes a lot easier to work with Dart as a statically type language. 139

But if you come from JavaScript or another language that has dynamic typing, just be aware that you can 140

also do the same with Dart. Now in the next lesson, 141

we're going to head back to making our apps and we're going to learn about stateful and stateless 142

widgets in Flutter.

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