All language subtitles for 6. Dart Basics

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

Now with our main.dart file now being totally empty, I will first of all walk through some programming

and Dart fundamentals before we continue rebuilding our app from scratch.

Now if you already are a more experienced developer, if you work with JavaScript or Java or C#, then

you will already know a lot of the things I'm going to explain.

Still, there will be some Dart specifics and therefore I would recommend that you don't skip this lecture

but instead maybe just ramp up that playback speed if it's boring to you.

If you're brand new to programming or you've never programmed before, then you should definitely

follow along.

Now for these basics, we'll not stay here in our project at the moment because we couldn't really execute that code

here since we have no working app at the moment

but instead you can google for DartPad and that is a web-based playground that allows you to write

some Dart code and run it in the browser

and that's exactly what I have here,

this is a little demo starting project which we started with but we'll write our own code here in a

second.

So this is Dart code, not a Flutter app,

just normal Dart code, you could also use Dart for web development,

this is just some dummy Dart code where we see the output here on the right in the console and it's

perfect for learning the basics about Dart.

Now what do we have here?

The overall thing we have here, this main thing, that is a so-called function. Functions are a crucial

concept in pretty much any programming language you're going to learn,

functions are code snippets which you can execute multiple times and at anytime you want. This function

here is named main because the thing in front of the parentheses here is the so-called function name.

The parentheses themselves are always required for every function you defined and they allow you to receive

so-called arguments that would be input to your function

and this function simply doesn't take any arguments,

nonetheless it needs these parentheses to turn it into a function.

Then we have curly braces,

the green ones here right now,

curly braces surround the so-called function body and that is the code which executes when this function

gets called

and I will show you how to call a function in a second. Void here,

that is a so-called type. Dart is a typed language which simply means that everything in Dart

has a type,

here that is the type of data which this function returns.

This function returns nothing and therefore the return type is void,

it's empty so to say. Here we see a number type and this strange for thing to which I'll come back later,

this would be the type of i, i holds a number, a so-called integer but more on that in a second,

let's not learn too much at the same time.

So this is a function called main,

it has no arguments and it returns nothing and therefore has a return type of void,

this is the function body. Main is a special function name, generally you can give your functions any

name you want but main is a special name because main is the entry point of a Dart application. Dart

automatically calls the main function for you when the app starts and

therefore in a Flutter app when you add main in the main.dart file which is also a special file for

which Flutter will look for, when you add a function called main here by adding parentheses and

by adding void in front of it,

this is the first function which Dart and Flutter will execute automatically when your app launches.

So into this function,

you need to put the code that basically kicks off that UI rendering work that brings something onto

the screen,

more on that in a second.

So that's a function, that's a function

and main is a special kind of function. Now because it's a special kind of function, you probably also

can create other functions, right

and you can. Let me first of all remove that code inside of the function and let's add a new function

above the main function and now I'll name this add or addNumbers, that name is now totally up to you but

you should follow a naming convention which is called camel case where you have only one word, so no

blank in between,

that would actually be a syntax error but you have one word

and if your word actually consists of multiple words, like this one add and numbers, then all words should

always start with a capital character even right inside of the word

but the first word always starts with a lowercase characters. So you have addNumbers,

if this would just be called add, then you would also start with a lowercase character but any other

word in there has an uppercase character, that's just a naming convention which we use in Dart and

also in many other programming languages which you should memorize and use, so that when other developers see

your code, they have an easier time understanding it. Now as I mentioned, a function can take arguments,

so an input.

Now the main function didn't take any arguments but let's say addNumbers is a function which should

hold some code to add two numbers and therefore this will now take two numbers as an input.

So between the parentheses here, we can add num1 and num2 and just as the function name,

these names are also up to you,

you can name that whatever you want and as you see here, you simply add commas as a separator between

your different arguments in that list of arguments here.

Now you also need a function body and you add that by using curly braces,

that simply tells Dart that in between the curly braces, you have the code that should execute when this

function gets called,

so when you instruct it to execute the code inside of addNumbers.

Now you do instruct Dart to execute this function by using its name,

now in the main function which is the code that first runs when the app starts.

So if here, in main, you simply call addNumbers by its name

and now here you also need to add parentheses.

You always need to do this, even if the function takes no arguments,

this function however does take arguments so now between these parentheses, you add the concrete values

which will be passed to addNumbers.

So the num1 and num2 do have actual data with which they can do their work,

so here we could add 1 and 2.

Now of course this function doesn't do anything at the moment though,

I call it with 1 and 2 here

but we have no logic in there to really add these numbers.

So what we can do in here is we can call num1 + num2 and now we would add that.

Now we also need to add a semicolon, that's another thing in Dart, for every expression you have in your

code, for every code line, you have to add a semicolon in the end, exceptions from the rule are the definition of

functions where you don't add a semicolon after this curly brace here but you do add it here for all

the code inside of your functions

so to say. And in general, for all these block statements as they are called,

so these statements where you have code between curly braces, you don't add a semicolon after the curly

braces, you only add semicolons after the expressions between curly braces.

So here we have num1 + num2, I added the semicolon here

but the problem we now have is we add two numbers here

and that would generally work but we're not doing anything with the result of this operation.

Now you could think that it should automatically be output here on the right,

well let's see.

Let's click run,

what happens is that we have an empty output here on the right.

Huh,

so that is not really helpful.

Now in programming, you always have to be very clear about what should happen because the programming

language, Dart in this case, does not make any assumptions.

Why would it print it here,

you don't tell it to do that and therefore it doesn't do it.

Now you can print the result by using the print function, print is not a function we defined, it's a function

that's built into Dart

and you can wrap

num1 + num2 with print

and if you now hit run, you will see three here on the right,

so now this works and now we're outputting the result of num1 + num2. So this is our first little piece

of Dart code and yet we can improve that,

for example we don't use types here but Dart is a strongly and a strictly typed programming language.

That means that you have to be clear about which type of data your arguments are or this function returns,

that is something which helps the Dart compiler to yell at you when you write code that violates that. If

you don't assign your own types which will do in the second, then Dart will assume that this is of the

dynamic type, a type built into Dart which as the name suggests doesn't tell too much about the type,

it basically accepts any value and therefore it won't really help you that much with checking that you're

writing proper code.

So if possible, you should avoid that dynamic type and assign explicit types when working with Dart. Now

there are a couple of built-in types in Dart.

Everything in general is a so-called object, which is a data structure that has some complex

logic inside of it,

possibly at least and we'll create such objects later too

but there are certain special types of objects you could say. There is text, for example and you create

text by using quotation marks.

So here if I type hello and I print this, please note the quotes, you can use single or double quotes,

that doesn't matter but you shouldn't mix them,

so if you open it with a double quote, you also have to close it with a double quote. I will use single

quotes in this course though and this creates a so-called string data type,

so this is a value which is of type string, text simply is a string.

If we now hit run, you will see hello being printed because

I'm printing this here and therefore we see the output here. So text strings are one data type.

Besides the text, for our numbers we have so-called integers and we have floats or doubles. Integers are

numbers without any decimal places, like 29 or 43 or -10. Floats or doubles are

numbers with decimal places like 29.99, -10.56, something like that. So

here, these two numbers would actually be integers.

Now in this function here, we should be clear about the fact that this only works with integers and

we do that by adding int in front of each argument.

This tells Dart that num1 and num2 both have to be integers.

So if I now run this, it will still work

but if I would pass 2.5 or 2.6 here, then we actually automatically get an error here in the bottom

right corner

that an argument of type double can't be assigned to a parameter, parameter is basically a different word

for argument, of type int

and that is why Dart does use these types, because it allows us to write cleaner and better code. If we

know that this function should only work with integers, then we should define it as such and if we then

later somewhere else in our code accidentally call that with a double, we get an error and we get that

error before we ship our code into the Flutter app,

so into the app stores for example and that is of course good, we want to catch and fix errors

whilst we are developing.

So here, we would get an error.

Now we have two possible fixes,

either we fix our code such that we only call that with an integer value or if we see no no no,

this should actually also work with doubles,

well in such a case, we could of course change that and accept doubles as inputs

and now if we run this, this code would work again and it would output a double here with a decimal place

because now we allow doubles here.

Now of course you could think, why don't we always use a double then, it's more flexible than an int?

Well sometimes you have situations where you really only want to have numbers without decimal places,

let's say you have some counter, which only increment by one at a time,

well then you should be clear about that being an int so that you can't accidentally run the code with

an invalid value with a decimal value.

Now side note, we could have also used num here which is the overarching number type and that would allow

both ints and doubles but as you see, if we use double here,

this also allows us to call this with a 1 because this implicitly is converted to a double.

So we can really use double here if we want to allow both double and int. So this works

and with that, we learned about text, so-called strings and about numbers where we have integers and doubles

and that all can be used as types which we for example pass to a function. Now this function however

might also return something or it might not

and you should therefore also always add a type information about what type of data

this function returns.

Now this function prints the result and therefore, it does not return anything to the place where we call

it,

hence the correct type here would be void.

Void is a special type in Dart

which basically means nothing,

this function returns nothing.

Now why is that important?

Because if we now for example wanted to print the result of addNumbers here, like this, then we get an

error that the expression here has a type of void and therefore can't be printed, we can't print the

result of us calling addNumbers because addNumbers doesn't return anything.

Sure, it prints something but that is done from inside the function,

here we would want to print what this function gives back and this function doesn't give us

anything back.

However, we could actually make sure that it does. Instead of printing in here,

let me comment this out what you can do by adding two forward slashes,

now this code won't be considered and this is a common pattern by the way, that you comment out code

which you don't want to run but maybe you want to use it later,

so you don't want to delete it but you simply comment it out so that it's still there but not

executed

and now we could return something here by using a special keyword in Dart, which is return. Now this is a

keyword which tells Dart that this will now return something to the place where you called the function.

So here, I return num1 + num2

and now what this does is

now here, this is valid code because now here we print the result of addNumbers and the result is what

addNumbers returns

and here we return two numbers.

If I now run this, we get an error though that we can't return a value because this expression has a

return type of void

and again, this is a good thing.

We defined that addNumbers doesn't return anything, that it returns nothing

and yet we do return something here.

Now the solution is to change the return type here

and here, we can change this to double because since we add two doubles, we will always return a double.

So now I'm returning a double here and now if I call this function and I run this,

now we don't get an error and we see 3.6 here instead.

This is how you should write Dart code, you should use these types,

you have the core types of string and numbers where we have integers and doubles with a couple of other

types too, which we'll learn about throughout this course

and this is a core concept of Dart, just as functions are a core concept of Dart because now we can call

addNumbers here as often as we want.

So I can also call addNumbers here with two 1s for example and

this is a really useful pattern and feature which you have in basically any programming language because

it allows you to write code once,

this logic for adding code and you can call it with different inputs

as often as you want and therefore, functions are a really important concept in programming and also

in Dart.

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