Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
We had a first look at some programming basics, functions and types or some of the important types
for the moment but there is more in programming and there are two other features which we need to understand
before we can go back to our main.dart file.
The first feature is the feature or the idea of variables.
Thus far what I always did in here is, I always immediately printed the result of addNumbers.
Now of course this is just some simple dummy code and therefore this make sense here,
we basically just want to see what this yields us here.
In reality however, you often write programs where you also need to store the result of some calculation
or some user input, you want to store it somewhere so that you can use it later.
Now with store, I don't mean in a database but in memory because you needed it later,
a couple of code lines later in the same code.
So we don't want to write it into a file or store it in a database,
we just want to store it in memory
so that when this code executes, we can take it and use it a couple of lines further down for example
and we can store data with the help of so-called variables. Let's say that firstResult here shouldn't
be printed immediately but instead I do want to store it in a variable.
You create a variable by giving it any name you want, like firstResult and the naming convention here
is just the same as for functions, you use camel case, lowercase starting character, then only one word
and every word inside the word starts with a capital character.
Now that's firstResult and you assign a value to that so-called variable now with an equals sign.
Now it's called variable because the data in there is variable
where you can change it, here I'm storing the result of addNumbers in firstResult, I could reuse that
and now also store that result in firstResult.
So after the first line, the result of addNumbers for 1 and 2.6 would be stored in here, after
the second line line here,
the result of addNumbers with 1 and 1 would be stored here and the firstResult, so this result of this
line here would be dropped,
we would store that result in the firstResult variable now.
However, we have a syntax error here and that means that Dart doesn't understand this.
This is almost the right syntax but to tell Dart that this is a variable, you need to add the var keyword
here
when you create that variable for the first time. So not in this line where I then use it again, the same
variable with the same name
but when you introduce it, then you need to add the var keyword to tell Dart that this is a variable
and now we can for example use that result, the firstResult, the name is firstResult,
we can use that here in a separate line.
Sometimes you even just use a variable to make your code easier to read, so that you can split it across
multiple lines,
sometimes you have some other code in between that you need to execute first and then you want to use
firstResult later.
Maybe you even want to combine it with some other calculation, that you add plus one to it,
that would be possible too. firstResult simply holds the value returned by addNumbers and that number
in our case is a double,
so you can do anything with it which you could do with any other double.
So now if I run this, we print three here because firstResult has the result of addNumbers with one
and one stored which is two and then we add one to it,
so we have 2 + 1 and we print that result and therefore see three here. Variables are a core feature in
Dart and we'll see it a lot throughout this course.
Now besides that syntax where I use var for creating it, you can also create it by adding the type of data
which this variable will hold eventually.
So in this case, we could also write double firstResult.
However, Dart also has a feature called type inference and
this means it's able to infer the type of data you'll store here by the value you have on the right
side. Here since I call addNumbers which returns a double and which always returns a double, Dart is
able to infer that firstResult will hold a double
and therefore you don't have to tell Dart that this is the case
and actually it's considered a better practice to then just use var. It would be a different thing if
you created the variable like this, without assigning an initial value.
That is possible too, the variable now exists but it is uninitialized and sometimes this is also something
you need to do because you want to create a variable and at a later point of time, you want to assign
different values to it. In such a case, it is a good practice to tell Dart which kind of data will eventually
end up in this variable,
so then you should use the type here instead of var,
in this case, that's the double.
If I now run this, we see the same output as before but now we created this a bit different and we are
using double instead of var
here. Now that's one of the important features which I wanted to mention, another important feature is that
Dart is a so-called object oriented programming language.
I did mention that everything a Dart is an object,
so this number, this one here, is an object. You would say it's a number and that's true but a number
is just object.
Now what is an object?
Everything in Dart is basically a data structure with a lot of different information in it.
Now here, the information is of course that it's one but there is some other metadata which Dart uses
internally
you could say. Now why do we actually start expressing our data structure as objects?
Well, that is simply related to the real world.
If we think about the real world, we work with objects there too, right?
You have a car and you can drive that car or your car has a certain amount of seats or a certain amount
of horsepower.
So we describe our real world with objects and the idea simply is to bring that way of thinking into
development, into programming so that it's closer to the real world.
So we work with objects in our program because of course typically with programs, like here where we're
building apps, we also have things which we could describe as objects,
for example in a mobile app we have a button, a button of course would be a valid object if you think
about it like this.
So that's why we call it object and where this idea of using objects comes from, why we group things into
data structures that we call objects,
that simply comes from the real world and it should help you think about and reason about your program
and the core idea really just is that you can kind of bring the way you think about reality into how
you think about your programs,
that's the rough general idea and how you work with objects will of course become very clear throughout
this course because we work with objects all the time in there.
Now you can also create your own objects because sometimes, you're not just working with a text or a
number but you have a more complex data structure which you want to express in your code.
Let's say you have a person with a name and an age.
Of course, we could create a variable here,
String name. String,
this is how you tell Dart that this will be of type String.
Please note that unlike double,
it starts with a capital character,
that's just how it is.
So you could have a String name and let's say an int age,
that would be possible and you could store values in there
and to you as a developer it would of course be obvious that these two are related.
However for other developers, it might not be
and even if it is clear, it's a bit annoying that you always have to work with these two separate
variables,
it would be nice if you could merge it into one variable and that can be done with so-called objects. To
create an object, you first of all need a blueprint for it that tell Dart how this
object should look like. You do this with a core Dart feature, which you also see in other programming languages
which is a class. A class allows you to define a blueprint for objects. A class needs to have a name
and that could be person
and here the naming convention is not to start with a lowercase character but with an uppercase character.
Then you add your class body with curly braces, just as you did it for the function and still this is
no function, this is a class, it's a different thing and in between that class, you can now define how that
class and therefore objects based on the class should look like.
Now what does this mean?
For example you can add variables in here, you can add a variable name and age if you wanted to,
so what I basically had down there is now added in that class and you can also give this default values
just as we did it before.
Now if you have a variable in a class, this is also sometimes called a property.
It's still a variable, it's just inside of a class and therefore we give it a special name
so that when I say we change this property, you know that we change a variable inside of a class. A variable
inside of a function on the other hand is called a variable and we have these different terms so that
when I say property, it's clear I'm referring to a variable in the class and not to a variable in the
function.
So here we have these two pieces of data now in our class and it is worth mentioning that just like
with normal variables in a function, if you are assigning a value which I'm doing here with Max and 30,
then you should actually use var here as well, instead of string and int.
Now I use string and int here because I'll change this later to not initialize it but if you would leave it
like this, if you plan on initializing a property with an initial value, then you should also use
var there instead of repeating the type. Repeating the type won't be an error but it is considered a
better practice to use var and let type inference of Dart do its job.
And now down there in the main function, if we need such a person, we can create a variable, p1 or whatever
you want to name it and you create a new instance of this class because the class is just a blueprint,
the instance of the class is the concrete object with which you're working by calling person like this.
Now if you have some other programming experience, you might be used to using the new keyword in front
of that, new is required in some other programming languages to create a new object based on your own
class.
Now that can be used in Dart 2 but since Dart 2 which we use in this course, it's not required anymore
and therefore we just call person like this.
So now we have person executed like a function actually with parentheses here and this will instantiate
this class and store it in p1 and therefore now if I print p1 here and I run this code, you actually
see instance of person being printed here. Now on this class, you can now conveniently access the
data that's stored in here with the dot notation, so I can type p1. and now I could choose name or age
or a couple of other things which every class has in Dart, automatically added by Dart but here I
could print p1.age and if I now run this, we see 30 here and that's what I meant.
Now we grouped that data together and we can create new persons by simply calling person again.
We can also change that data on a person by accessing p2.name and changing that to Manu for example
and now if I would print p2.name down there and I print p1.name up here, so for the two different
persons, I print the names, then you see that here on the right, we print Manu and Max because for
the first person, I didn't change it and therefore we have that default, for the second person I did change
it and therefore here where I changed it, we change it to Manu, down there when I print that, I print
Manu. This is how that works and classes are a crucial feature in many programming languages and they
also are in Dart. Now there is more about classes and about Dart which we'll learn throughout this course,
I don't want to bore you now with a one hour long introduction to Dart,
I find it more interesting to learn Dart whilst we are learning Flutter but these are some basics which
I needed to get out of the way, so that we now can continue working on our main.dart file and understand
what's going on in there.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.