YouTube Video — Transcript

This Flutter tutorial covers control statements like if, else if, else, and collections in Dart with practical coding examples.

Key Takeaways

  • Use if, else if, and else statements to control code flow based on conditions.
  • Always prefer using curly brackets for clarity in if statements.
  • Strings containing quotes require escaping or alternative quoting styles.
  • Final variables cannot be reassigned once initialized.
  • Hot reload in Flutter helps quickly see code changes without restarting the app.

Summary

  • Introduction to control statements in Dart including if, else if, and else.
  • Explanation of final variables and their immutability.
  • Detailed syntax and usage of if statements with curly brackets.
  • Demonstration of one-liner if statements without curly brackets and their caveats.
  • How to chain else if statements for multiple conditions.
  • Discussion on Dart string literals and handling quotes inside strings.
  • Explanation of escaping characters in strings and using double quotes to avoid confusion.
  • Importance of line numbers in code editors for collaborative coding.
  • Brief mention of hot reload in Flutter and its effect on the build function.
  • Encouragement to follow the course chapters chronologically for better understanding.

Full Transcript — Download SRT & Markdown

00:00
Speaker A
Hello everyone, and welcome to chapter four of this Flutter course. In the previous chapter, chapter three, we talked about keywords, data types, constants, variables, and some basics of functions. In this chapter, we are going to, as you can see on the captions here, focus on control statements and collections. Now, I'm assuming you're following these chapters chronologically, so that you're going one by one. I'm assuming then that you have your Dart project set up and that you've got some sort of a simulator or emulator running. So, without further ado, let's have a look at—I'm actually gonna bring up the captions here, and then I'm gonna put my face at the bottom there and bring up our project that we created in the previous chapter up here. So, this is running at the moment. So, if I do a command S or control S on Linux and Windows, then as you can see here, there is a run button, and then it's just gonna do a hot reload, which in turn calls this build function, which we don't yet know really how it works because I haven't really explained it. But what we did, we just called a print function here and then our getFullName function. So, what I'm gonna do here is let's remove this code from the build, and let's remove everything that we wrote here as well. So, keep the main function as you can see here, and then keep your StatelessWidget, which we're gonna talk about later. Okay, so instead, go and create a new function that is called void test. All right? And then in the build function, just say test, all right? Just like that. The purpose is that in this function, we're going to write most of the code in this chapter. So, we are going to basically test some code. That's the purpose of this function. So, let's talk about if and else if and else. They are control statements in most programming languages that allow you to branch your code based on a condition. So, if we, for instance, in here, create a final variable whose value cannot be changed, if you remember from the previous chapter, whose value cannot be changed after it's been assigned to. Let's say final name is foo, all right, and end it with the same semicolon. Now, an if statement basically allows you to check the value or a condition to be of a certain value or to be true or false, and then it allows you to execute some code. And then you can also append an else statement to it, which gets executed if the code or if that condition is not met. I'm going to show you how that is written. The syntax for an if statement is just if, and if you think is a keyword, so you would say if, and then you would open and close parenthesis, and then you would put curly brackets like this. That's the syntax of if. And in the parenthesis, you would put your condition. So, I would say in this case if name, and I would like to, for instance, check if name is foo. Now, we haven't come to operators yet. We're going to talk about operators actually soon. But this is, as you can see here, we're saying final name equal to foo. This is kind of like an assignment operation, and that is an operator in Dart. It's called an operator. So, there is also, and since this is an assignment, we don't want to actually assign anything to name. We want to check if it's of a certain value, and that is with this conditional operator equal equal. And then you would just say, okay, if it's foo, then for instance print yes, this is foo, and then a semicolon to end it. However, if this condition is not met, you may want to execute another piece of code, and then you can just do else like that, and then this would, you would just say no, this is not foo, all right, and a semicolon. So, I'm just gonna do command S, and you can see the value or the string yes, this is foo printed to the screen or to the terminal here or to the debug console. You can have multiple statements in here, so you can have more print statements in here. You can just do anything you want basically in here, and the same goes for else. Now, there is a possibility to write if statements kind of without the curly brackets, but you don't see them that often, and I recommend that you actually always try to, as a convention, do with curly brackets if you can. However, it is possible that inside a code base that you will work on with your colleagues, it is decided that it's okay to have if statements without curly brackets where they are very simple and one-liners basically. So, let me show you an example of that. You could just say if name is foo, and you would just say print yes, this is foo, and this is completely accepted as well because it's a one-liner. But if you then say, okay, I want to have another line print hello world, you can see it goes to the next line, meaning that this line right here will be executed no matter your condition up there. So, this is completely detached from your if statement up here. Okay, and I just realized that it is quite important for me to be able to show line numbers, and by default, your editor probably has line numbers enabled. I usually disable line numbers when I'm working myself basically, but when talking about code with other people, it's quite useful to have line numbers because I can then tell you, oh, look at line nine or look at line ten. Okay, so that's the basic of if and else. Now, you can also have if right after else. So, an example of that is, for instance, to say if name is foo, and then you do some code, all right? You say print yes, it is foo, not food. After the curly bracket, then you would say else, and then you can then say another if, else if, all right? And then after if comes the parenthesis and a curly bracket if you remember from the if before. It is the exact same syntax as it is here, so there's pretty much no difference. You can see if, and then if, and then you put your condition in here. You would just say if name, and then you may just want to say is not, and that's another operator. It's basically an is not operator, so it's checking to make sure that this name is not of the value that you provide to the right-hand side. And then you can say bar and print this value is not bar. Else, and then you can wrap it up, and you can say print I don't know what this is now. I'm so glad I actually accidentally ran into this issue. As you can see, now the editor is kind of going crazy, and Dart's not understanding what this statement is. And as we talked about it previously, strings in Dart are preferred to have to be kind of like created with single quotes. However, if your string in itself contains a single quote, for instance, in this case, the word don't contains a single quote, then Dart kind of gets confused like, okay, does the string end here? You can see it's up until this point is green, so it's thinking, oh, the string ends here, but what is all of this then that follows? If you have that problem, there are two ways of fixing that. Either you escape your single quote, and that is a software kind of development lingo escape kind of. If you hear that, it pretty much just means that you are kind of wrapping it in a way that Dart understands that you don't mean it literally. You don't mean that I want to end my string here, except that this needs to be escaped and put in the string as its own value basically. It's kind of difficult to explain, but I think you know what I mean. So, that's one way of doing it. It's a little bit dirty, so in case you run into this issue, it's best to actually wrap your entire string in double quotes, and this way then Dart understands that whatever comes in here is okay. Like a single quote is completely okay unless you actually want to have a double quote in your string that is enclosed with double quotes again. So, you'll see if I put a double quote here, then Dart goes crazy. So, you can escape it with a backslash like that. So, if you have single quotes and double quotes in your string, then you will have to kind of pick a convention either you enclose the entire string in single quotes and escape your single quotes inside that string and let the do—
00:16
Speaker A
you can see on the captions here we're gonna focus on control statements and collections now um i'm assuming you're following these chapters um chronologically so that you're like going one by one so i'm assuming then that you have your uh dark project set up and
00:36
Speaker A
that you've got some sort of a simulator uh or emulator running so without further ado let's have a look at um i'm actually gonna bring up the captions here and then i'm gonna put my face at the bottom there
00:53
Speaker A
and bring up our project that we created in the previous chapter up here so this this is running at the moment so if i do a command s or control s on linux and windows then as you can see here there is a run
01:07
Speaker A
button and then it's just gonna do a hot reload which in turn calls this build function which we don't yet know really how it works because i haven't really explained it but what we did we just called a print function here and then
01:22
Speaker A
our get full name function so um what i'm gonna do here is let's remove this code from the build and let's remove everything that we wrote here as well so keep the main function as you can see here and then
01:36
Speaker A
keep your stateless widget which we're gonna talk about later okay so instead go and create a new function that is called void test all right and then in the build function just say test all right just like that
01:55
Speaker A
the purpose is that in this function we're going to write most of the code in this chapter so we are going to basically test some code that's the purpose of this function so let's talk about if and else if and l's
02:09
Speaker A
are control statements in most programming languages that allow you to branch your code based on a condition so if we for instance in here create a final variable whose value cannot be changed if you remember from the previous
02:27
Speaker A
chapter whose value cannot be changed after it's been assigned to let's say final name is foo all right and end it with the same semicolon now an if statement basically allows you to check the value or a condition
02:44
Speaker A
to be of a certain value or to be true or false and then it allows you to execute some code and then you can also append an else statement to it which gets executed if the code or if that
03:00
Speaker A
condition is not met i'm going to show you how that is written the syntax for an if statement is just if and if you think is a keyword so you would say if and then you would open and close parenthesis and then you
03:15
Speaker A
would put curly brackets like this that's the syntax of if and in the parenthesis you would put your condition so i would say in this case if name and i would like to for instance check if name is fu
03:30
Speaker A
now uh we haven't come to operators yet uh we're going to talk about operators actually soon um but this is um as you can see here we're saying final name equal to foo this is kind of like um
03:46
Speaker A
an assignment uh operation and that is an operator in dart it's called an operator so there is also and since this is an assignment we don't want to actually assign anything to name we want to check if it's of a certain
03:59
Speaker A
value and that is with this conditional operator equal equal and then you would just say okay if it's foo then for instance print yes this is foo and then a semi-colon to end it however if this condition is not met you may
04:20
Speaker A
want to execute another piece of code and then you can just do else like that and then this would you would just say no this is not foo all right and a semi-colon so i'm just gonna do command s
04:36
Speaker A
and you can see the value or the string yes this is food printed to the screen or to the terminal here or to the debug console you can have multiple statements in here so you can have more print statements in
04:48
Speaker A
here you can just do anything you want basically in here and the same goes for else now there is a possibility to write if statements kind of without the curly brackets but you don't see them that often and i
05:03
Speaker A
recommend that you actually always try to as a convention do with curly brackets if you can however it is possible that inside a code base that you will work on with your colleagues it is decided that it's okay to have
05:17
Speaker A
if statements without curly brackets where they are very simple and one liners basically so let me show you an example of that you could just say if name is foo and you would just say print yes this is
05:34
Speaker A
foo and this is completely accepted as well so because it's a one-liner but if you then say okay i want to have another line print hello world you can see it goes to the next line meaning that this line right here
05:52
Speaker A
will be executed no matter your condition up there so this is completely detached from your if statement up here okay and i just realized that it is quite important for me to be able to show line numbers and by default your editor probably has
06:06
Speaker A
line numbers enabled i usually disable line numbers when i'm working myself basically but when talking about code with other people it's quite useful to have line numbers because i can then tell you oh look at line nine or look at night line
06:21
Speaker A
ten okay so that's the basic of if and else now you can also have if right after else so an example of that is for instance to say if name is foo and then you do some code all right
06:42
Speaker A
you say print yes it is foo not food after the curly bracket then you would say else and then you can then say another if else if all right and then after if comes the parenthesis and a curly bracket if you
06:59
Speaker A
remember from the if before it is the exact same syntax as it as it is here so there's pretty much no difference you can you see if and then if and then you put your condition in here you would just say if name
07:13
Speaker A
and then you may just want to say is not and that's another operator it's basically is a knock operator so it's checking to make sure that this name is not of the value that you provide to the right hand side
07:30
Speaker A
and then you can say bar and print this value is not bar else and then you can wrap it up and you can say print i don't know what this is now i i'm so glad i actually accidentally ran into this issue
07:50
Speaker A
as you can see now the editor is kind of going crazy and darts not understanding what this statement is and as we talked about it previously strings in dart are preferred to have to be kind of like created with single
08:04
Speaker A
quotes however if your string in itself contains a single code for instance in this case the word don't contains a single code then dart kind of gets confused like okay does the string end here you can see it's
08:20
Speaker A
up until this point is it's a green so it's thing it's thinking oh the string ends here but what is all of this then that follows if you have that problem there are two ways of fixing that either
08:33
Speaker A
you scape your single code and that is a that is a software kind of development lingo escape kind of if you hear that it pretty much just means that you're um you are kind of wrapping it in a way
08:53
Speaker A
that dart understands that you don't mean it literally you don't mean that i want to end my string here except that this needs to be escaped and put in the string as its own value basically it's kind of difficult to explain but i think
09:06
Speaker A
you know what i mean um so that's one way of doing it it's a little bit dirty so in case you run into this issue it's best to actually wrap your entire string in double quotes and this way
09:17
Speaker A
then dart understands that whatever comes in here is okay like a single code is completely okay unless you actually want to have a double co double uh double quotes in your string that is enclosed with double quotes again so
09:32
Speaker A
you'll see if i put a double code here then dart goes crazy so you can escape it with a backslash like that so if you have single coats and double quotes in your string then you will have to kind of pick a convention either you
09:45
Speaker A
enclose the entire string in single quotes and scape your single quotes inside that string and let the double quotes live or you enclose your entire string in double quotes and then you will escape your double quotes in the string
10:02
Speaker A
i hope i hope that makes sense so that is um if and else in um in a nutshell but before i move on i kind of want to explain an important concept here in that when dart executes this code it
10:18
Speaker A
kind of goes by line by line so it starts with line seven the execution of this test function then it comes here and it says okay name is foo all right um and it's actually saying that we prefer
10:30
Speaker A
const so let me change that to const okay so um and it says okay if name is foo all right then it comes over here but let's just change that to foo with triple o um actually yeah let's no let's not do that let's
10:48
Speaker A
keep it as foo so it says if foo is if name is food then it says yeah that's true and then it goes here however as you can see the line number 11 also says if name is not bar
11:04
Speaker A
then it should execute this as well and and you can see that foo is not bar so this condition is also true however this this this will never be executed and that is because when dart goes inside these curly
11:20
Speaker A
brackets then it says okay i did what i had to do i did my print statement the rest i ignore so if an if statement is kind of like a is a it puts a stop when it when it falls
11:32
Speaker A
into one of these if statements it kind of says okay i did my job in this entire if else block then i will execute the code after that so if i clear the console here and press command s you can
11:44
Speaker A
see it only says yes it is foo it doesn't come here so however if you change this um condition so that the dart compiler or basically the execution of the program doesn't fall into line number 10 it will
11:59
Speaker A
eventually fall to line number 12 so you can see it says this value is not bar so when you're writing your if statement and else statements just be careful with that just know that it's kind of like a
12:10
Speaker A
as soon as the code jumps into one of those branches it doesn't go to the rest of the branches and checks them basically all right okay um that was for if and else now let me check that in my notes that we've talked
12:30
Speaker A
about that um what we need to do now is to talk about um operators operators um they there are three different types of operators there are prefix operators infix operators and suffix operators and i will explain them to you i mean how
12:58
Speaker A
they work i think to explain operators probably it's best that we talk about numbers so let's go in this test function and delete everything that we we've written here and let let me just say final age is 20 all right so
13:15
Speaker A
um now we created a variable whose value cannot be changed after a spin assigned to because we created as a final a variable whose value is 20. all right now if you want to calculate half of this value how would you do that then
13:30
Speaker A
well there is an operator for that and that is a division operator so you would just say for instance final half of age as you can see we're using camel case as i explained in the previous chapter an easy way to do camel case is to say
13:46
Speaker A
half of it just writing in english change the first letter of uh of like this here change the first letter of all the words except for the first word to uppercase and then remove all the spaces that's camelcase
13:59
Speaker A
basically so half of age is age and then there is a great operator in a dart called a division operator and then you would say two all right and then you could just say print half of h so command s and that says 10. all right
14:18
Speaker A
so that's the division operator and then you will have um let's say final double the age and then we'll say h multiplied by two it's kind of like a star that is an operator and you can see these both operators this one
14:40
Speaker A
the division and the multiplication uh multiplication it's a little bit difficult to say operators are in fix operators and an infix operator is an operator that has two parameters one to its right and one to its left i don't know how you can
14:55
Speaker A
actually yeah one two one to each side i don't know if my video is like mirrored so if my right hand is actually right hand on your side as well so i don't know uh yes it is probably
15:08
Speaker A
so these are infix operators when they have two values one to each side however there are also prefix operators and an example of that is for instance if i said final h minus 1 and actually we change this to var i'll explain soon why
15:36
Speaker A
and if we said minus minus h now this is an example of a prefix operator and a prefix operator is an operator that it comes before whatever it has to do its work on and in this case this minus minus operator what
15:54
Speaker A
it does is that it takes the value that comes after it it decreases that value by one and then it returns its result back to the left-hand side so in this case the the interpreter comes here and says okay
16:11
Speaker A
you want to calculate something okay it's equal to oh a prefix operator that takes a variable here and it says okay what is age is 20 minus 1 is 19. puts it in here and it also puts that minus 1 sorry puts that
16:27
Speaker A
19 in an h so i think so at least so if we say print age and print age minus 1 now both should kind of be 19 and you can see it says twice you see 2 19. so that kind of means it's uh twice
16:42
Speaker A
so flutter has this great ability not to duplicate basically log statements or princess statements so we have i mean there are some really great operators in dart and that is the plus operator operator and then you have the minus division multiplication and
17:02
Speaker A
then you have the logical kind of like operators to check if a value from the left is equal to the value to the right so these are kind of like the basics of the operators in um dart so you don't have
17:13
Speaker A
to know so much more about these for now i would say um you can get really far with these and a lot of these operators actually work not only on numbers but also on strings so and for instance if you this is one of
17:29
Speaker A
the cool features of dart which i absolutely love i think it was just such a great idea for them to implement this like uh one thing i mean if you're not coming from a software development background um you may not know this but if you're
17:43
Speaker A
coming for instance from design background also you would notice that sometimes when you're creating designs or any any screen and you want to kind of like show that design to someone like a product owner you want to actually populate the text
17:56
Speaker A
inside your design with some lorem ipsum and lorem ipsum is basically has a huge history of where it comes from but it's usually some sort of a dummy text kind of that you want to place inside your design so that you can just like display
18:10
Speaker A
and show it to someone and a lot of developers like in many languages when they want to display some text and they don't know what to say like we're just testing stuff i just want to display some text they go and
18:21
Speaker A
grab lorem ipsum and then put it in their code but dart has gone to the next step and said okay we don't need that so what you can do you can say for instance name is foo that's a string name but you can say
18:35
Speaker A
name times 20 is name times 20. you can literally say multiply a string by 20 and what that does is that it says foo 20 times so if i say print name times 20 you can see it says
18:52
Speaker A
20 times so it's a great feature you can say foo bar pass and then just say times 100 and yeah it just that's it copies it for you 100 times in names time 100 for instance so so that is a um
19:13
Speaker A
that is shortly said that is uh the basics of operators in uh dart okay i'm gonna delete that code gonna mark in my notes that we've talked about that now i'm just gonna quickly also mention that um we can also create custom operators to
19:42
Speaker A
be honest with you and i don't think it is completely all right right now to talk about custom operators i think it's kind of like a topic to talk about later so i actually just moved it in my notes that
19:56
Speaker A
we don't need to talk about it right now because we have to know back a little bit about a little bit about classes and objects so that we can after we've created our own class then we can create
20:05
Speaker A
custom operators as well so i don't think it's completely appropriate to talk about right now but just know that in the future you can also create your own operators and then like or you can also override like different operators and how they work
20:18
Speaker A
with different objects so it's it is really fun um but that's that's that let's just let's just leave operators to that for now now the next topic that i wanted to talk about is lists and a list in dart is
20:39
Speaker A
a series of things that are similar to each other and they are placed inside a list as you would have for instance a piece of paper and you could write your shopping list or like a christmas shopping list or whatever
20:56
Speaker A
and it's numbered so um these are called lists in dart so for instance if you say uh foo and then bar and then baz as you can see dart doesn't like this syntax at the moment it says okay what are these like what is this uh
21:18
Speaker A
comma here the way to tell dart that this is a list of things that are like in place one two three is by placing them inside uh square brackets and then a semicolon of course now um as as i've talked about it before just
21:39
Speaker A
just now actually lists have indexes in that they all have their own place placement inside the list all these objects as we call them they have their placements inside the list and you may think that object number one is
21:55
Speaker A
foo and then object number two is bar and now number three is bas and i kind of correct but index says in i mean all the programming language that i've worked with c plus plus c rust dart swift
22:13
Speaker A
javascript they pretty much all start at the index of zero and that is so important to understand because like the placement of this object called foo inside this list is not one it's actually it's index is zero and that is why lists are called zero
22:34
Speaker A
based and their indexes are called zero based indexes so you've got to be careful with that and we're going to talk about indexes soon actually so let's just say final names is that now if i want to extract foo from here i'm just going to
22:50
Speaker A
say final foo is names at the index of zero right and i'm just going to print it i'm just going to say printfu and you'll see foo printed here now if i say i want to get paths then i'm going to
23:04
Speaker A
say print sorry index of three and you'll see oh sorry index of two because it's the third item but since indexes are zero base then it's in excel so i made the mistake myself so command s and you'll get bass
23:22
Speaker A
and i actually want to go back to this three and then command s so you can see what happened here you'll see you you'll get something called a range error which is an exception an exception in dart and many other
23:37
Speaker A
programming languages is when things go wrong so that the language doesn't really know anymore what you mean and in this case you can see this through an exception at the language level or at the library's level where it
23:51
Speaker A
says there are no four items in this array or in this list because the index of three indicates four items item number one in the list has the index of zero item number two has the index sub one item number three has the
24:06
Speaker A
index of two and item number four has the index of three and there are only three objects in this array or in this list so zero index zero one two so that's it and index of number three is
24:24
Speaker A
non-existent and that is why we're getting this invalid value not in inclusive range zero to two so that's how you access items inside a list and this is how you actually create a list okay so you say list of items which
24:42
Speaker A
is foo bar bas so you can also there are some convenient um properties on lists that you can for instance say final length so if you want to know how many items are in this list you can also say
24:59
Speaker A
names dot and then you would say linked we're going to talk a little bit about that now as well because i think it's so important to understand what that is dots in dart and in many other programming language is a way to drill
25:15
Speaker A
in to something to extract something else from it and in this case names is a list and all lists in darts have something called properties length is a property of the list data type in dart and that means every list in dart has a
25:40
Speaker A
property called linked that is automatically calculated for you you don't have to do anything as you enter values in this list this length is gonna then return the right value to you so i'm gonna i'm just gonna talk about that
25:55
Speaker A
a little bit more so i'm just going to say print names links it's going to say you'll see here let me scroll a little bit it'll say three three items in this array i'm gonna change the names list to var
26:08
Speaker A
so that we can actually um change its contents and mutate it like i just want to add a new name to this names and i'll just say add it's a function as you can see because it has parentheses right after it
26:25
Speaker A
so and i will say um my name and then semicolon to end the statement now if i say print names length right after the statement you guessed it oh i can see actually my um you may not see all the print statements
26:41
Speaker A
because this text is right there so let me resize my visual studio code a little bit so you can see things a little bit better sorry about that so now we have names length here and you can see the second time around it said
26:57
Speaker A
four okay so when you say dot after the name of a of a variable and that means it can be a constant a variable or a final variable after you put dot after its name you're accessing different properties or different
27:16
Speaker A
functions inside that thing okay so it's kind of like an accessor it allows you to drill down inside an object and grab things out of it or maybe make that object do something for you okay so i think i think that's good enough to be
27:33
Speaker A
honest with you about lists i don't want to go too much into details and scare people about like all the different things you can do with lists and but there there is great documentation on dart's own website all you have to do
27:46
Speaker A
is just to google or duckduckgo or whatever you want to use bing your your way through and just search for dart arrays documentation or just dart arrays and then you will find lots of information about it or sorry dart lists because in dart
28:02
Speaker A
they're actually called called lists in some other programming languages they're called arrays but those names can be used interchangeably to be honest with you lists and arrays are kind of like yeah almost the same thing so all right um
28:18
Speaker A
that was lists in dart i'm gonna take it in my notes so that we know we've talked about it the next topic that i wanted to talk about in this video are things called sets all right and as you can see in the caption i've
28:40
Speaker A
written here is a list of unique things whereas lists were lists of homogeneous things sets our list of unique things so i think the best way actually to explain sets is just to jump right into it so the syntax for a set is with a curly
29:02
Speaker A
bracket uh actually i think i think it is yeah i think so so let's say final names i'm just going to say curly bracket foo and then end that okay just like that now if i type names here
29:22
Speaker A
you will see that the suggestion provided here or the information provided by visual studio code says names is a set of string we talked about this in the previous chapter that when i say final names or final something name for instance is
29:38
Speaker A
foo i'm kind of telling dart to create a variable whose reference name is name and its value is foo and dart automatically understand that the data type of this value is string because its data is a string so you don't have to say final
29:58
Speaker A
string name although you could do that but let's change this to const as well as and you don't have to actually say this is a string because dart understands it automatically so you just remove the data type if that's what you want to do
30:14
Speaker A
which is what i actually prefer to do instead of being too verbose unless you have a really good reason to provide the data type anyways let's go back to sets so we were here means and that's and in the same way
30:30
Speaker A
that we created a string and then dart automatically understood the data type by putting a square bra sorry curly brackets here or curly braces here and putting some data inside that dart automatically understand okay this is a set of some stuff in here and this stuff
30:45
Speaker A
for now just looks like a string so if i say foo bar as art says okay this is fine but if i go in here and i say foo again you can see i automatically get then an error
31:00
Speaker A
message say saying two elements in a constant literally cannot be equal because it understands that this foo it has already repeated there okay so let's remove foo from here and let's do an experiment let's change this names to var so that
31:19
Speaker A
we can actually change its con its contents then go to name and add to the next line and say names if i can spell and say add and then say foo again and then say names add bar and then set
31:35
Speaker A
add bass or sorry means add bass and then we'll say print names okay and i'm gonna clear the logs with this button right here and then i'm going to press command s and you can see that the set is still
31:52
Speaker A
full bar bass nothing changes really there that is one of the greatest properties of list of um sets in dart and in pretty much every other programming language that supports sets and that's sets only ensure always that their data
32:12
Speaker A
is not duplicated and this is i mean there's lots of magic happening in the background that i haven't really talked about in i mean for instance like the question that you may ask how does dart know foo is the same as foo does it compare
32:28
Speaker A
them yes it kind of does actually um it's internally doing a comparison between these things but we never told it how that comparison should work and that is because if you remember from operators when we talked about it the string
32:47
Speaker A
um now we haven't talked about classes well i'm just going to say strings in dart already know how they should be compared with other strings okay so they they kind of have this logic of hash codes and comparison operators that they
33:06
Speaker A
know exactly like okay given this string am i the same as that like given sorry given what i am and this new thing that you're providing me like i'm foo and you're saying foo am i the same as foo and then it says yes
33:20
Speaker A
or no so that is what sets are doing internally but we don't actually see that so um just know for now that sets in dart allow you to create unique list of things okay and then you may also be a little bit like
33:40
Speaker A
tempted to go and say okay i have things here let's say const things is equal to foo and then we say one and this will also work and that is because dark has the concept of object as well you can
33:57
Speaker A
see you'll see things and it will say oh now it's a setup object we haven't talked about objects yet and if i look at my notes i can see that we're going to talk about objects in chapter 6
34:10
Speaker A
and we're right now in chapter 4. so we haven't really gotten to that point to talk about objects and i don't want to really scare you about objects and what they actually are but just know that there is a hierarchy of data types
34:27
Speaker A
so you have object and then you have data types that kind of derive their functionality from objects so here sits object and then here's his string integer double etc and then you have sets and blah blah blah now
34:45
Speaker A
when you're here when you were first here and you said i have foo barbaros all strings then dart was like okay this is a set of strings but then you said oh i have a string and i have
34:57
Speaker A
an integer which is a number now dart was like oops i have i can't i mean i don't really i can't say this is a list of strings and integers that's not possible in there so it says i'm gonna
35:08
Speaker A
take the common denominator type that sits above them as their parent to specify what these things are okay and that is kind of like the same thing that we do in real life in that if i give you for instance um
35:24
Speaker A
uh two candies like a twix bar then you say then you would say this is a twix bar that's one thing if i give you 10 twix bars then you will say a lot of twix bars or 10 twix bars
35:37
Speaker A
if i just give you a bunch of twix bars you know oh that's a bunch of twix bars but if i go and like blend like lots of twix bars mars bars and whatever all the sweets into a bag
35:52
Speaker A
and i give you that bag what do you call it you don't say oh this is a bag of twix and sneakers and this is a and mars bar no you would probably just say candy it's a bag of candies
36:05
Speaker A
and that's what dart is doing here saying oh you threw a lot of stuff in here i i can't like comprehend that this is a set of uh objects so we do that in real real life and that's
36:17
Speaker A
what dart is doing here so just when you see object just know that oh dark doesn't really know what this is anymore so or it kind of knows what it is it's just a bunch of stuff so that's what object in this case means
36:34
Speaker A
so that was a weird uh comparison maybe but i hope you get uh what i mean and if you see me looking here it's just because i'm looking at my notes to ensure that i've explained the things that i've
36:46
Speaker A
set out to explain so talked a little bit about hash codes and sets we don't have to go into details about that so now let's go and talk a little bit about maps and maps are kind of one of my favorite data data structures
37:07
Speaker A
in any programming language that supports them pretty much almost all modern programming languages support maps so a map is a data structure as you can see here maps are used to hold key value pairs of information and what that means is
37:26
Speaker A
let's say that you want to explain a person using their different properties then you would say age their gender their hair color their heights whatever name all that so these are kind of like your keys so you would explain
37:53
Speaker A
the properties of that person using those keys and all those keys have their values so if you say height then you say like 180 yes and if you say weights then you select 70 kilograms so these are key values the keys are the
38:11
Speaker A
properties of that person and the values are the values of those properties so and the way to create a map is very similar so to very similar to a set with a curly with curly brackets so you say person
38:26
Speaker A
and then you say equal to actually this is just a name you don't have to say person but yeah or you can just say const so person and then you open curly brackets and then you would do your keys here and i would say the
38:38
Speaker A
key of age for instance i'll say 20 and then you say column is it calling yeah it's a column and then you end the whole set with a semicolon so i just created a basically here i created a map whose
38:57
Speaker A
keys are string and values are integers now let's see if dart understands that you see this is it's a map of string as keys and ins as values but now as you saw before the analogy of candies now if i say okay i have another key and
39:14
Speaker A
it says um name and i say oh the name is foo dart is going to be like oop what is this person oh it's a map of string as keys and object as the values because it just looks at the common
39:31
Speaker A
denominator of the string which is foo and 20 which is an integer it says oh i don't know i can't create a map whose keys are string and its values are both string and integers so i'm just going to
39:44
Speaker A
go to their parent and be like oh the parent of both string and integer is object so your values are of type object so a bunch of candies basically okay um that's how you create a um you create a map in dart so
40:02
Speaker A
and the other property of a map is for instance you would say um if you if you go and create an age again here you see it says two keys and a constant map literally cannot be equal so it's
40:16
Speaker A
doing some checks here making sure that the keys are actually unique so keys inside a map need to be unique all right so however if you go and change this and say var person so and then you later go
40:31
Speaker A
and say person name is equal to foo like just like that and then you print the entire person so i'm just going to say print before and print after okay and you can see here it said first time
40:50
Speaker A
h key is 20 it has a value of 20 and the name k has a value of foo with a capital f then the second time around on the print statement on line 15 then we when we print the person it has
41:03
Speaker A
the key of age with a value of 20 because we didn't modify that and a key of name as it was before with the value of foo with like six i think uh capital o's so this is how you would modify a
41:17
Speaker A
map you would specify the key and then you would say equal to which is an operator and then you would say the new value all right um however if you added a new key here for instance um last name
41:32
Speaker A
and you say bad and that will just kind of like get appended to the um to the map so age the same name the same now you have a new key whose value is bas all right and that is kind of like the basics of
41:50
Speaker A
um maps so there are lots of things you can do with maps and i really encourage you just like everything else that i talked about in this chapter that you go and read some of the documentations because if you're following along with this
42:04
Speaker A
course and your goal is to become a software engineer and if you're for instance a project manager or a designer ux or whoever you are um i think you need to understand that software engineers do not know all the answers you have to
42:19
Speaker A
go and read documentation you have to practice practice practice and put time into it countless countless hours you need to put into learning so i i mean this course i think is gonna be so many hours long the the way i can i mean i can see
42:38
Speaker A
uh the planning for all the chapters that i put here there's so many chapters i think the entirety of this course is going to go over 20 hours so you can imagine i can't go into details about every single thing
42:51
Speaker A
otherwise it is just this course is probably going to be like 200 300 hours uh it's unbelievable so we can't do that so what i expect you to do now is just to open your browser and just type dart
43:04
Speaker A
maps documentation something like that dart maps so and you also need to know that now that you're starting to google things you need to be aware of the word dart and that dart is an existing very popular name for just darts
43:21
Speaker A
darts okay so sometimes depending on what you're googling if a topic related to the classic darts that you throw is more popular than the topic of the programming programming language which kind of sounds the same it may pop up first so
43:39
Speaker A
in this case if you say dart map you may actually end up in some sort of weird website that explains to you how you can throw darts at a map i don't know i'm just trying i'm just trying to bring it up because that i
43:53
Speaker A
actually ended up in that situation that i searched for something related to dart i actually ended up i mean a weird website talking about darts as like a sport so know that as well please all right now we've uh talked about maps
44:08
Speaker A
so i'm gonna take that in my notes um now what's coming in the next chapter we are gonna talk about a very um important concept in dart and many other programming languages such as rust and swift and that is null safety or dart
44:31
Speaker A
calls it sounds not the sound safety it is a very important thing to know about in dart and it will greatly help you in the future as you write your flutter applications and in this course i'm going to use a lot of
44:47
Speaker A
null i'm going to make a lot of gnaw references talk about null quite a lot throughout the course so it is very important that we go through that as soon as possible and we're definitely not gonna leave that out so
45:02
Speaker A
that's gonna be for chapter five so keep an eye out for chapter five that is to follow so i hope that you enjoyed this chapter chapter number four where we talked about dark control statements and collections and i'll see you in the
45:17
Speaker A
next chapter
Topics:FlutterDartcontrol statementsif elseelse iffinal variablesstring escapinghot reloadFlutter tutorialDart programming

Frequently Asked Questions

What is the purpose of using if, else if, and else statements in Dart?

They allow branching of code execution based on conditions, enabling different code blocks to run depending on whether conditions are true or false.

How can I handle single quotes inside strings in Dart?

You can either escape the single quote with a backslash or enclose the entire string in double quotes to avoid syntax errors.

Why is it recommended to use curly brackets with if statements?

Using curly brackets improves code readability and prevents errors, especially when adding multiple statements inside the conditional block.

Get More with the Söz AI App

Transcribe recordings, audio files, and YouTube videos — with AI summaries, speaker detection, and unlimited transcriptions.

Or transcribe another YouTube video here →