Dart language tutorial 2021 | Dart programming full course for beginners and advanced

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this course is based on the official dart documentation and the amazing book dart apprentice so have you ever heard of terms such as null safety factories interfaces inheritance abstract classes meek scenes asynchronous programming singleton patterns and somehow you don't know what they mean or how to use them or maybe you never heard of those terms or maybe you never wrote a single line of code in your life well regardless of what's your case this course is for you if you are watching this course chances are that you are interested in flutter development and one of the most common problems with us flutter developers is undermining the importance of really understanding how dart works has in our flutter development careers helping you understand the power of dart and how dart works is the reason why i created this course we are going to start this course from ground zero i will show you how to get the dart sdk up and running into your local machine and then i'll show you how to download and install the text editor that we are going to use called vs code after that we are going to have an introduction to programming we will define concepts such as variables functions control flows and loops i will also take some time to teach you best practices in terms of software development such as drive then we are going to talk about collections and operations that can be done into collections different types of collections in dart and we are going to discuss when and how to use each one of them after that we will have an introduction to object oriented programming we are going to define classes and objects and different types of classes we will talk about constructors the different types of constructors and when or how to use them we will briefly talk about design patterns where we are going to build a singleton class after that we will talk about more advanced stuff such as inheritance types of inheritance we will talk about interfaces abstract classes mixings and so much more another topic that is going to be super important is null safety and nullability we are going to explain what's that null safety and why is it so important at the end of the course we will talk about isolates and asynchronous programming i will explain you how it works in the context of dart and these are just some of the topics that we are going to touch in this course course is made for developers in any level you can be a developer who never wrote a single line of code in dart or you can be a flutter developer that has one or couple apps already built i believe that there's a thing or two that you can benefit from by watching the course the lessons of this course are constructed independently from one another that means that you don't have to watch the course sequentially you can skip to the lesson or section that interests you hopefully i was able to convince you to enroll to the course and if yes see you inside now depending on when you watch this video some stuff here might be outdated because dart is a language that's evolving right but that's not a problem because you can watch the updated version of this course for free on skillshare and what skillshare you ask skillshare is the sponsor of today's video and it's an amazing online learning platform where you can go to improve your skills they have classes on a variety of topics from design to photography to finances coding and so much more if you sign up using the link in the description you'll get a month of premium access for free and that's valid for the first thousand people to sign up using the link in the description and you can do that and access this course and my other course flutter web and yeah that's a very good deal if you ask me thank you skillshare for sponsoring this video before we start the course a quick note here is that you can find all of the source code here on this repo and the link will be linked to the description of the course or the description of the video once you have clicked on the link you can just click on this button here and download the zip file and after you have downloaded the zip file if you open the project one folder and open the bin folder you can see that we have a bunch of folders here basically for every every single lesson of the course you'll have code associated with the lesson and you can open the respective file here to follow along this is going to be super useful if you face some bugs because you can use this to kind of troubleshoot your code when it comes to running your dart applications or your direct programs you have two options you can run them from the browser or you can run them from your local machine if you run them from the browser you do not have to install nothing you just have to go to a website and it's called dartpad.dev this one here and you can run your dart code from your mobile phone from your tablet from your computer it doesn't matter the other option is uh downloading the dart for the dart sdk installing some software called ides that will allow you to actually run the code on your machine for this course you can follow along on this website let me open up the website by the way and this is the website the dart pad website you know and here you have a template called template dart code i don't know if you guys are able to see because this is super small but it doesn't matter and here is how you run here you get the results so as simple as this you come to this website and every single thing i am going to be showing and teaching you guys on this course you can just run everything here right but i would advise you to actually go ahead install the sdk download all of the software and uh develop your programs on your machine right and uh to do that you can click here on install sdk and this will take you here all right this is the official dart uh website the first thing for you to know here is that if you already have flutter installed and flutter is a framework that was built using dart you do not have to install the dart sdk because by default flutter from the version 1.2 comes with dart installed right you only have to explicitly install dart sdk to your machine if you want to build dart apps that are not flutter apps right if you're trying to do this three things here right uh on this course we are going to build a dart app that it's not a flutter app so we need to actually download dart uh depending on your system you will have different instructions here on how to install dart on your computer if you are on linux you just have to copy these commands here super straightforward super simple if you are on windows you will have to run this commands here and if you are on mac os which is my case you'll have to run this command here the thing here is for example for windows you'll have to install this unfortunately i am not running a windows machine now and i do not have a way of showing you guys this but it's super straightforward you just have to click here on install and uh just click next next next you're going to install everything and after that is installed open the command prompt cmd copy and paste this command here and this command here and this will get you ready to write your dart code all right linux same thing for mac os users first of all you have to install homebrew i already have this installed so but all of these are are super straightforward steps to be honest copy this code paste it to your terminal i already have it installed so i'm not going to pa to paste it but you would copy this and paste it here hit enter and continue right basically to get everything up and running this is what you would have to do now after you have installed dart on your machine what's the next step the next step is actually checking if dart was installed successfully if dart is installed on your system you just have to open your terminal on windows you would have to open the command prompt right and to do that on windows again is the start button and then type cmd that will allow you to open the command prompt after you have that the code you're going to use to test is darth version so there was a typo here darth version and here you have the version two point twelve point zero you may have a different version depending on when you watch this tutorial and follow this step if you have an error here that basically means that dart is not successfully installed on your computer what you can do is you can send the screenshot of the error on the q a section and we can help you troubleshoot the issue well now you have your dart sdk installed on your computer and you are ready to go the next step here is to download visual studio code in case that's the text editor or that's the software we're going to use to run uh the code so you just have to download here depending on your computer you may have something different here and depending on when you're going to watch this tutorial this website may be changed but that's not important as long as you can find the download button you have to download that after downloading you will get an executable file you just have to install this software super straightforward as well just have to double click on the downloaded file and then hit next next next next next and you will have vs code installed now um if you open your vs code you may have something similar to this or something extremely different it doesn't matter right now right and this is like this because i changed the theme and i've been working with vs code already and probably are not going to have all of these options here as well but don't worry don't have to worry the only thing that you have to do here is clicking here on extensions and then you will type dart click on install i already have it installed right and what uh this will allow you to do is allow your vs code to run your dart applications right so this is extremely important you have to follow this tab install the the dart extension that will enable you to run dart code on your vs code right so this was super super simple super straightforward nothing too complex nothing to worry about and um that was it for this lesson we'll continue on the next one on this video we are going to run dart code now that we have the extension installed into vs code we can start running our dart programs and to do that it's super simple what you will want to do is just select file then open this will allow you to open a new file and what you want to do is create a new folder and this folder is where you will save all of the files of the whole class the whole course right it doesn't matter where it is located on my specific case i already have a folder created and i called it dart course as you can see it's an empty folder right let's create what's called hello world hello world basically is the first thing you usually write on a programming language now here in vs code to create a new file you just add we just have to click here you have new file if you hover on this button you have a new file and the other option is to do file new file or command command n or control n right since we are working with dart files the extension has to be dart basically here you have the name of the file then you have dot dart and this is what tells the ide or what tells the this software that this is a dart file good the icons you are having or the icons you use on the vs code that you are using may be different because here we are using a custom team a custom theme the hello world program that we are going to write here will print hello world right you just have to code along with me don't worry about not understanding this here is where your dart code starts every single time you start a dart program you will have this function here and since we want to display something we need to print so just write print and what we want to print is hello world here you have the code now you have a lot of options to run this code first of all you need a command s to save you can click run and if you do so you can see here hello world another option is clicking here run code hello world or you can press f5 right so this is basically it this is how you start with your dart program but this is a single file let's imagine you want to create a dart project and usually you will want to create projects and the advantage of projects is that they come with a built-in structure already made and how you do that it's super simple click on terminal new terminal make sure that the the directory here the last thing is dart course so this directory here is the director that you're currently in here and then to create a dart project you type dart create and the name of the project and remember that the name of the project can be anything here we are going to call this project one and if you have a look here you'll see that we have a new folder called project one now to access this folder just need to open up this here and as you can see here we already have a bunch of files and directories here we are going to go through this on the next video don't worry about that but if you open the bin folder you'll see that we have a file called project1 the same name as the name of the project you can click on this file and run it and it does the same thing you can write your name here just to make sure you are running this here not the previous code here you have it right good now um we have all of this done what we need to do now is to understand this structure here because most often than not we will need to create dark projects not single files i can even delete this so on the next class on the next lesson i mean i will explain you the structure of a dart project and the use of of everything here and now we are going to understand this as the class progresses don't worry about this for the moment just be happy because you have your project here up and running people people welcome back to the course on this video we are talking about prod project structure so here we have a dot dart tool and here we have package config basically this is a configuration file if you open this file you'll see that you have some information relevant to this project like the project name the root of the project the version of dart used and all of that stuff but usually you're not going to need to open this because you're not going to change nothing we have the bin folder or directory and the bin folder contains the dart files that we're going to run on our case we have here project1.dart and this file was created automatically and it has the same name as the name of the project then we have git ignore if you are planning on hosting this project on on a platform such as github or another git platform you may need to ignore some files if you want to host the project sometimes you don't want to publish or make all of these files available so you're going to ignore some of them for example here we're ignoring the packages dart tool and the build analysis analysis options.yamo what this file will do this contains some logic that will help to analyze your code to the code you write the dart code you write and find errors if you have any errors on your code and this process is called linting right change log on this file here let's suppose you build this project you have version one and then you build a second version of the project where you change couple features on this file is where you would keep track of all of all of the features that were changed and everything that's new you would keep track of that here you would manually insert those uh changes here we have pop spec yamo this is a configuration file as well and it keeps track of the packages external packages that we use on the project and not just that we have here the version number of the sdk the version of the sdk of the project description of the project and the name of the project readme file this is a very popular one and uh this is basically a file that has a description of the project of what the project does or how to use the project so yeah that's basically it that was a very rushed overview but i think that that's everything that you need to know as far as this structure goes hello beautiful people welcome back to the course well uh we've been working with visual studio code for like two videos now but i would like to do one video which is this lesson here where i basically explain the windows the different windows here on visual studio code i think that would be very important important for this class for this course right the first thing that we have here is called the activity bar and depending on what you select here this will define what's displayed on the sidebar and here is the sidebar if you click here you can hide the sidebar or display the sidebar the search for version control here is how is where we run the code for extensions and when you install new extension sometimes you will have new icons here on the activity bar right and this is our sidebar so we have here activity bar sidebar uh depending on the operating system that you're using on windows you usually have a top bar here as well just like this here we have this window where is the code usually not usually actually here is where we write the code and this is called the editor and it makes sense because it's where we it's where you edit the code and down here we have the panels and here we have debug console you can open the terminal outputs you can check for problems as well at the very bottom of visual studio code i don't know if you guys will be able to see on this video but if you open the if you open the software you'll be able to check the status status bar at the very bottom right so this is visual studio code and those are the windows that you are going to be working on right yeah so this was basically it see you guys on the next one this is an update video that i had to add at the end of the course depending on when you watch this course if you click on pubspec.emo file the sdk version here might be different what i want you to do is change the sdk version that you have to this one here so just copy this line here as it is exactly like this when we start talking about nullability and null safety you'll be able to change to the version that you currently have and don't forget to save the file after you have updated this line here hello beautiful people welcome back to the course on this video we will talk about statements expressions and operations right let me create a new file inside of a folder just to organize this let's call it one and here i'll create a file and that's how you usually name files in dart right uh well let's start let's start with uh statements so what's a statement in dart statement is an instruction it's a command with a statement you're basically telling the computer to do something to execute something and such example or an example i can't find is print for example we already worked with this you may need to write everything inside of a main function you can remove this it's not needed so everything is going to be done in here i will write comments here and commenting basically is the ability of of describing code in human understandable way and what's special about commenting is that for example if you add this the compiler is not going to run this code is not going to recognize you usually use comments to describe what a code or piece of code does right and to use comments in dart you just have to start with double slashes and then you can start writing the comment on that line so we have here statements again as i was telling you guys a statement is a command here we are telling the computer to print something and in dart every single statement ends with a semicolon if you delete you are going to have an error and here you have it so basically here we are telling the computer that this statement starts here and ends here i can let me copy and paste so the computer will understand that these are two different things that this is one statement and this is another other than statements we have expressions and uh don't worry about this messages we i'm getting this because because i have some extensions that you guys do not have you probably are not getting this so don't worry about them expressions are not commands expressions are things they do nothing for example if i have a number here let's say 4 like this this is an expression right it's a basic it's a number if i have a text here saying this is an expression so basically expressions are things that do nothing all right i know it's a bit hard to understand but yeah it's basically that now it's not every single time that a statement will end with a semicolon this is only for simple statements if you have complex statements i can add here complex statement statements something like this i mean i don't even have to write an example we can have here i can use this file the this function the main function as an example this basically tells the computer where to start executing the code but at the end of the statement we do not have semicolons that that's because we have this carly braces and usually when you have curly braces you do not have you do not need to add semicolons and if you do you are going to get an error because it's not expecting to get a semicolon so that was it about expressions and you can have things operations like this one here and this is this is an expression right now let's talk about operations and when i say operations here i'm talking about automatic operations just like this one and for the examples that i'm going to provide here we are going to do this on numbers only just like in math we have the basic ones here you can add you can subtract you can divide multiply so here we have addition subtraction multiplication and division right let's do something nice if you run this code nothing is going to happen but if you want to see the result of this you'll have to paste copy this and paste it inside of print statements right now here we have it let's run the code first of all you have to save it and this is how you save click here to save everything what happened here is that i ran the code but nothing changed but that's because this is minimized i just have to drag it like this and you can see here hello world hello world too one plus 1 is 2 and all of this is correct now something that i want to show you here where we have 4 divided by 3 you have this number sometimes you just want to get the integer value and to do that to get the integer division you just have to add this sign here before the division symbol and the result is going to be one as you guys can see here so this was it in terms of expressions statements and operations and these are the basic ones you can do a whole bunch of mathematical operations here but let's stick with the basic ones most likely you are only going to need to use this once so see you guys on the next one hello beautiful people welcome back to the course on this video we will talk about variables constants and a lot of stuff in between but before we start on the previous lesson we talked about operators arithmetic operators i just want to remind you guys that automatic operators are not the only type of operators but i don't want you to feel like you have to memorize a bunch of stuff my goal here is to make you understand so we will we will talk about other types of operators as we need them right so we have this here this is an example of an expression and we talked about this on the previous lesson what happens is that most uh times you will need to access this value somewhere else in the code right and having to type one plus two every single time in the result of this expression is time consuming so we have um containers let's call them containers that can store values right and on those containers we can classify them as variables and constants so variables think of variables as containers that can store values of the same type but can change the value and constants containers that can store values of the same type but the values cannot change right so let's start with an example so here we have one example this is a variable and what's happening here is that we have a type of notation here we are defining the type of this variable this is not needed to do in dart but i feel like it's important for me to start by showing you this and then i'll show you other ways of doing stuff but usually the structure is the same both for constants and variables first of all you have the type of value that you're going to store and here int stands for integer so we want to store an integer number which is the name of the variable and here we have the value right so we here we have assigned this expression to this variable and here we have the equal sign and this is called an assignment operator because you assign values to variables or constants here and as you can see when we print here we have seven and three so what's nice about this is that for example we can do something else and as you can see now to access these values the seven and one plus two now we just have to reference the variables or the name of the containers now here i created another variable called result 2 which will be equal to the first result plus the number which is this but we can actually change the value of a variable so if i come down here and i type result 2 will be equal to 10 for example if i run this code if i run this code let me change the value here so that you guys can understand what's happening so if i run the code here we have the result of the operation here result plus number which is 3 7 which is equal to 10 but after that we are manually assigning a new value to this variable result 2 which is 4 that's why here when we print we have 10 which is the result of this expression here and then we have 4 which is the value that we assigned manually here we have the most common data types in dart unlike other programming languages dart doesn't have what's called as primary data types because every single data type here is an object right if you're new to programming that didn't make any sense but don't worry you will understand more about objects in future lessons but anyways for numbers for representing numbers we have int for integers double double for decimal points and num num can store both types and num is is the parent class of the int and the devil we have boolean to express values as true or false for example i have here do i love dart and the value is true we have strings to represent text and characters and we have lists to store a list of things and then here i have some basic rules of naming variables because as you can see there there is some rules this is not random variables variable name can consist of letter and alphabets keywords are not allowed to use as a variable name what's a keyword keyword is basically a name that it's already taken right in simple terms for example we have bull here the dart compiler already understands bool has a data type and you cannot use that data type as the name of your string so you cannot use names that are already taken by the language blank spaces are not allowed obviously first character of a variable should always be an alphabet just like here we always start with alphabets variable names are case sensitive that basically means that having name here in lowercase letters and have name in capital letters is not the same special characters are not allowed you cannot have hashtags here the add symbol and all of that the only special character or one that it is one that is allowed is the underscore and another one on how you for example if you see here you have do i love dart and the way you write these long variable names you use what's called a camel notation so you start with a lowercase but when you start a new void you have to start start with the capital letter just like here so this was variables in a nutshell let's proceed about type safety type safety basically means that once you define a variable type you cannot just change it afterwards if you define a variable to be a string you cannot then assign an integer to that variable on the first example here we have string my name which is santos if i uncomment this line here you'll see that we have an error and the error says the value int can't be assigned to a variable of the type string and this is super awesome because it's going to save you a lot of times and avoid a lot of bugs right but on this case here we have fee which is of the type num and the type num can be both an integer and can be a double right so here we have the fee which is 10 and then we assigned a new value to the fee which is 7.7 no problem now you can define variables without having to have here the data type explicitly right and as i told you the keyword to do that is var so we have here var weight is equal to 3.14 but what happens here even though we didn't define this as a double or a num it automatically takes that type if i uncomment here boom we have the type string cannot be assigned to a variable of the type double now if you want to be able to change the value the data type of the variable you use another keyword called dynamic so we have your weight which is 3.2 but you can change that to weight is equal to car and then you can change that to weight is equal to true right but usually you won't use dynamic to define data types right it's safer to go with a var or to have here the actual data type if you know what the data type is and now let's talk about constants constants are variables that do not change they're they're also known as immutable variables and the previous section where we're talking about the other variables uh known as mutable variable all mutable data and this here is immutable data now to define this unchangeable variables we use two keywords which are const and final right and the difference here is that const is used to define constants when we know the value to assign to the constant beforehand also called as compile time constant and final we define constants when we denote we do not know the value beforehand here we have an example of a compile time constant what's going to happen here we have constant value right constant this is called type interference you don't have to define you don't have to say constant then in you have to type sorry you don't have to type const then intend the name you just have to have here const then the name of the constant and then the value to be assigned and dart automatically will know that this is an integer constant so we have here const value right because we know that the value of our constant will be seven if i uncomment this line here we have an error because the constant variables can be assigned a value now let's imagine we don't know the type or the value that we're going to assign to our variable we have here current date this is a special dart function that will return the date you don't have to worry about this now we will have a look at it in the future but basically what this does is it returns the date and the time obviously it's impossible to define this manually because you don't know when will the code run or where or what time is it going to be right so we are calling this and we are assigning the value to this final so when this code run get the actual date and time and assign to this constant and print it if i run this here we have the current date and time and it's correct today is the 30 of june 2021 so this was basically it see you guys on the next lesson now let's talk about data types well in dart we can work with different types of data as we saw previously we can work with strings numbers booleans and so much more so data type is basically what sounds like is the type of data in a very simple way the root data type or the main data type in dart is called object and every other data type is a subtype of this data type we know that if for example you define a variable has a string you cannot assign an integer to that data type but there's something called type conversion for example let's suppose you want to store this integer h in a variable of the type string you would have to convert this from an integer to a string like this and if you wanted to convert from a string to an integer or double you could do something like so here we have what's called type conversion if you want to store a certain data type into a variable of a different data type you would have to convert that and for the car for the case of the string you just have to call tostring all right and this is how you call a method that's defined inside of an object but that's a bit advanced don't worry about understanding this but this is basically how you do it and if you want to convert from a string to a double and you have to make sure that it's convertible it's a number not character this is how you would do so this is what we call type conversion so far we've been performing arithmetic operations only on integers at a time or booleans at the same time right but we can in fact perform operations on integers and booleans and sorry not booleans double and many other different types now i'll i will just bring you an example of a basic arithmetic operations or operation on an integer and a boolean so we have here an integer variable called integer value which is 3 and we have here a double which is 2. actually this is a double and the compiler will understand this has 2.0 because it's a decimal point number now we have a third variable here called operational operation result which multiplies the integer value with the double value now my question my question for you here is what would be the type of the operation result and the answer is double every single time you operate doubles with integers the resulting value will be a double right because it's simple doubles can store every single integer values but integers cannot store double values so if this was to be an integer we would have a lot of issues here but to test what i'm saying we can run the code again to run the code to execute the code we need to have the main file the main function sorry so the first thing we are going to do in here is print the result and on our case the result is going to be equal to operation result to string remember to have a tostring converted to string and to find the value or the data type of the variable we will print the results type is and then this is how operationresult.runtimetype this is everything we need actually this is what is going to return the actual runtime type but we then have dot to string to make sure that we can print this and as i was saying if you have a look here the result is 6.0 not just six and the type is double now let's talk about typecasting imagine that you have a value of a certain type and you would like to use a property but that property only belongs to the subtype of that type i know it was a bit hard to understand but let me explain it again you have here a value a variable of the type num and you want to check if 7 is an even or odd number right integers values of the type integers have that property if i try to directly print here value is even i will get an error saying that the getter is even isn't defined for the type num so what i would have to do here is the following create another variable called value integer and we want to use value as int not as a number or num so basically this is type casting welcome back on this video we will talk about strings and couple string operations uh as always we need the main function here because that's how we are going to run the code let's start with defining a string actually you have two ways of doing that using single quotes and double quotes and you have single quotes actually both are single quotes but the idea here was to have single quotes and double quotation marks really there isn't much of a difference and um it's not like you're going to have a problem for example on this uh third string we have here ones one since we want to use the single code sign here it's required for us to have double quotation marks defining our string here good another concept is concatenation concatenation basically means adding or joining strings together and for our example here we can have um another string let's call it fourth string which will be equal to the first string plus the second string plus the third string and what happens with this sign here the plus sign sign when you are dealing with strings this is going to join the strings together when you are dealing with numbers obviously it's going to add the numbers if we were to print the result of this and as you guys can see here we just joined all of this one one ones one this is what we have here this is not the only way of doing it let's have another example let's define another variable so what this means basically is that this variable is going to be equal to the same contents of the variable plus the fourth variable right and this is called an increment operator right when you want to add a value to an existing value you use this combination of symbols here plus equal if you want subtract that would be minus equal running the code gives us this output here and as you can see we joined this string here with the value of this here and again writing this is the same thing as doing something like this so having this here is the same as having this here so this line here is equivalent to the line up here but it's faster to write it this way obviously and somehow easier to understand now let's talk about interpolation interpolation in simple terms is the process of replacing a value inside of a string with a placeholder basically if you want to display a variable inside of a string you would use interpolation and how you would go about doing that is like this let's imagine we have the variable here called name so if you want to display the name of the user right we can just call the variable here you use the dollar sign symbol before the variable name so instead of writing here my name is santos you just have my my name is name which is the name of the variable and if we run the code you can see here let me you can see that we have here my name is santos and if you change the name of this variable you can see that the value changed here now we have my name is homie right and this is not just for strings actually let's imagine we have this expression here age and we want to print the age we have here the age is equal to the value here if we run this code we have the result here age is 24. now multi-lines to use multi-lines here or a string with multiple lines you use three codes instead of just a single quote or quotation mark depending on how you call it so let's have an example here of some dummy text so what's different about using multilines is that when you print the code the structure of the text is kept you can see that if we run the code here the structure of the output is the same as the input here there are countless string operations for example let's imagine we want to convert the name here santosh to uppercase you would have something like so what happens after you have name which is the variable name and you click dot you have a lot of options here to create a substring to uppercase lowercase stream and all of this and these are basically methods of the string object again you don't have to worry about that now because we will talk about that in the future just wanted to show you that there are a lot of methods for string objects and if we print this you can see that here we have santosh in capital letters so if you want to dive deeper into string methods or string operations there's a lot of good content out there we have this blog here i will link it to the to this lesson you can read it so they explain how to use a lot of methods here right but again i don't want to just give you guys a bunch of information and i don't want to make you feel like you have to memorize stuff we are going to use them when we need them on this class we will talk about boolean operations and boolean operators and boolean logic we have talked about operators before but only automatic operators and now we are going to talk about boolean operators before we even start talking about operators we should understand boolean data type so i have here a small definition and says that brilliant value can have one state of two true or false so basically a boolean data type is a data type that can store only two values or true or false so the states can only be true or false here i have two boolean constants and we are going to use this on our examples the the one with the true value we call it yes and the one with the false value we call it no boolean operators boolean operators also known as comparison operators are operators used to compare values let's start with equality we use the equality operator to check if two expressions or two values are equal and the symbol is double equal sign just like this a single equal sign is called assign assignment operator and this is called a comparison operator or equality so here we have our we define another constant another variable called yes is no so what's happening here we are checking if yes is equal to no and obviously this should resolve to false right and the parenthesis here are not needed you can write this without parenthesis but to make it easier for you guys to understand i will include those here if i run this code we have here yes is yes equal to no and this is false because yes is not equal to no now we have another operator called inequality operator and you can use this operator to check if two values or expressions are different basically and the symbol is the exclamation mark followed by the equal sign the exclamation mark is also called not operator so basically this means not equal and here we have another example is yes not no and this should be true we are printing here the value let's see and as you guys can see is yes not no i should change the text here is yes not no which is true just remember to change the text here now you might be wondering what is this backslash n this is called the new line operator actually the new line symbol right if you don't include this for example you're not going to get this blank line here that's why we use this so as i was telling you guys the not or the exclamation mark is also called not operator right and toggles the value so if a value is true and you proceeded with the not operator it's going to be false another way of having this expression here would be something like this so here we have the expression same thing just remember to change the text to make sure that this makes logic or makes sense it's logic now let's talk about greater than or less and lesser than operations i believe that you guys are very familiar with this symbol from math and the example here would be let's have two variables one called one and the second one called two now the first thing we want to check is is one greater than two and this should be false and then we want to check is two greater than one if one is smaller than two means the two is greater than one so is one greater than two is two greater than one so for the for for the first example as you guys can see here i know that the output is a bit small but hopefully you guys can see this is one greater than two it's false is two greater than one it's true now we have another symbol here which is lesser or equal than or greater or equal than right so you will have the lesser than and greater than symbol followed by an equal sign so we are checking on the first example is one lesser or equal to two and on the second example is one greater or equal to two so we are going to print that is one lesser or equal to two true is one greater or equal to two that's false now let's talk about something called boolean logic boolean logic is a form of algebra which is centered around three simple words operations or and and not sometimes you want to combine more than one boolean expression for example you want to check if if one is greater than two but you want to check as well if two is greater than let's say five let's start with and and operator is only true if both operands are true what this means is this can only be true if the value of yes is true and the value of no is true as well since noise false and this is true the result of this expression will be false let's run this code and as you guys can see here end of yes and no is false now we have the or operator and our operator is true if at least one of the operands is true so what that means is or of true and false is true and the symbol are these two i don't know how to call these things here these two bars and the symbol for the and operator is the end sign ampersand sign so let's run the code and but this should be or sorry or of yes and no is true and now another thing i want to show to you guys is you see how these two outputs are close together and that's because we do not have this slash and new line here now continuing we have the node operator and i already talked about this and what happens here is that if the value of an expression or a variable whatever it is is true and we proceeded with the not operator the value will change from true to false or if it's false it will change from false to true so basically saying not true is the same as saying false which is true here we have the example the opposite of true is not true and this should be false another thing i would like to show you guys is how i have this nature inside of the dollar sign and this curly braces when you want to print an expression you have to insert the expression inside of carly braces just like this and if you run the code we can see the output here it says the opposite of true is false which is true now there is something else called operator precedence in an expression with multiple operators which one will be executed first let's have this example here and this looks complicated but it's not so we want to check the value of this expression and basically the expression is 3 is lesser than 5 and 2 is greater than 1 or 3 is equal to 4. can you guys try to guess which is going to be the output of this expression so the result of this expression here is true we have something called a table of precedence and it's something like this basically what this means is that in an expression the not operator has the higher precedence basically this means that this will be executed first then we have this operators here then we have this this and the last thing to be executed is the or right the same way you have operators precedence on arithmetic operations because you already know if you have let's say addition and multiplication the multiplication will be done first right but this is something you can override and i will show you guys in a second how to do that so overriding basically is defining for yourself which section of the expression will be executed first and you do that using parentheses for example we have this expression here the parentheses are at the back and again since we want to print the value of an expression we have to insert everything inside of curly braces so here the parentheses are back here and here the parentheses are in front what's going to happen is that the value or the result of these two expressions will be different and if you analyze and if you have a look here you can see that the results are different because here we first did this so the value of 1 is lesser than 2 is going to be true 1 is lesser than 4 is true as well and this is going to be 2 right but if you check 3 is greater than 4 this is false and then we are going to do false and true which is going to be false thus we have false here now coming back to this example we are going to execute this section first so 3 is greater than 4 is false 1 is lesser than 2 is false as well so this whole section is going to be false but this one is true so we are going to have here false or true which is true hopefully that was easy to understand and let's proceed hello beautiful people welcome back to the course on this lesson we will talk about control flow and as the name suggests uh you use control flow to basically control the flow of the execution of the program right most often than not you will not want to execute everything every single thing one after the other for example if you want to check if a user of your app is under age so the for example he cannot access certain parts of the app right you would check for the age if the user is under age you would do something if the user is not on the age you would do something else that was a weird example but okay now to make that possible we can make use of the following methods first there's something called the if statement if statement allows the programmer to tell the compiler which section of the code to execute based on the condition so what that means is that you will check if a particular condition is true you are going to execute some code let's suppose we have here age right so this how this works is like this if and this must be uh an and a boolean expression right it must be an expression that returns a boolean value a condition if age is lesser than 18 we want to do something in here so on this example it would be if age is lesser than 18 we want to print you are under age now if you have a look here nothing happens why because 21 is not lesser than 18 but if i change here the condition and have something like this and run the code again you can see that now we have you are under edge because it doesn't make sense really but basically i just wanted to show you since 21 is greater than 18 this is not going to execute because this condition is not true now there is something called the else clause and this almost every single time you send if statement it's going to be followed by an else clause it's not required but usually is like that so let's suppose we have here the conditional statement we have if the age of the user is greater or equal to 21 we want to print you can drink else basically if the age of the user is not equal or greater than 21 we want to print something else and on this case we want to print you cannot drink if we execute this code you can see here that the output says you can drink because in fact 21 is greater or equal than 21. there's something else called the else if statement sometimes you want to check more than one single condition and let's proceed with our example like this first we we are checking the following if the age of the user is greater than 18 and is lesser than 21 we want to print here you are off age but you cannot drink right considering that the drinking age is 21. or we want to check something else that's when we use the else if and here we have the following if the age of the user is greater than 18 and is greater or equal to 21 you want to print you are of age and you can drink if we check the output here we have you are off age and you can drink let's see here we have else just drink water kid if i change the age here from 21 to let's say 15 and run this code we have here the outputs first for this section here you cannot drink you're underage first it's here you were under age when we execute this action here the output is you cannot drink and finally we have just drink water kid now here we i will teach you something called variable scope and in very simple terms variable scope is basically where is that variable visible or where can the variable be used right let's write some code and you will understand that for example here we have if age is greater than 18 right we want to we we just declared a variable inside of the if statement note that this is inside the if statement want to print hello the name you are of legal age we execute the code this is the output hello santos you are of legal age if i try to access this variable the variable name outside of the if statement let me uncomment this line now we have this error saying undefined name name which is the name of the variable basically we are saying that the compiler is telling us that this variable is not defined right and you might be wondering how is this variable not defined if i have it here the problem here is that this variable is defined inside of this local scope here so it's only visible and accessible inside of the if statement everything outside this curly braces cannot see this variable thus cannot access it and that's that's what's called scope of a variable now let's talk about something called ternary conditional operator the majority of the operations we had now uh to be honest all of them they use two operators right ternary as the name suggests we use three different operators and on this case this is very similar to an if else statement because the way it is structured is like this we have the condition here on the left side then we have the question mark and the first expression here will be the result or will be the value if this condition is true and the second here will be the value of this condition is false here we have an example if age again the the parenthesis are optional if age is greater than 21 we want to print you are of age else we want to print your underage right and if we execute this code we would see here you are of age because the value of age is 21 and 21 is greater than 19. another good thing we can do with the ternary operator is that we can assign values to constants or two variables for example i have here variable called can i vote and we have age is greater or equal is greater than 18 right we want to check this condition if this condition is true the value of the variable can i vote will be yes and if this condition is false the value will be false or no so can i vote and as you can see here the value of can i vote is true if i change this the value here can i vote will be false now let's talk about switch statement switch statement are usually used can be used as a replacement to multiple conditional statements or to multiple if and else statement and the structure or the form or formula i don't know how to call it but the structure of switch statement is like this we have switch and here we have a variable and the difference here is that it doesn't take a condition it must be a variable and every single case for each case here we have a potential value of this variable so what we are doing is if the value of this variable is equal one of these values we are going to execute some code and after we are done executing the code we have this keyword called break basically we are telling the compiler that we executed everything that we had to execute and now you can break right and we have the default keyword here basically this means that if all of these values are not equal to the value we are checking here just execute the default code here let's have an example and you will understand this let's suppose we have a variable called menu item number on our case is one and here is our switch statement right inside of our search statement we are checking our menu item number case the value is 1 we want to print open profile page then break case the value is 2 we will print open settings page then break and the default is invalid option basically this means that if we have any other value here that is not one or two we will print a valid option if we run the code you can see here at the very bottom that we have open profile page because the value here is one if i change this to two as you can see now we have open settings page and if we change this to six for example the last value here is invalid option now let's talk about nums or enumerated types so noms are data type that allow you to create your custom types with a limited option infinite number of options right and these are usually used with switch statements and an example of that would be something like this this is how you define an enum you start with the keyword and um the name of the enum on this case is menu item and notice that you start with the capital letter you have your menu item and here you have the elements or the types of this ename on our case is profile and settings now we are going to create a constant and this constant will be of the type menu item because this is the main is a custom type that we just created and the default value of the constant item is going to be menu item dot settings so what we are going to do is we are going to pass this to a switch statement and for the first case we want to check if the menu item is equal to profile we want to open the user profile if the menu item is equal to settings we want to open user settings and default will be invalid option if we run the code you can see that here we have open settings page and that's because the value here of the items is menu item dot setting so this was basically it for control flow i know that probably too much but feel free to watch this lesson again if you feel like there's something you need to understand better and feel free to ask me any question if you have beautiful people welcome back to the course on this lesson we will talk about loops now let me ask you a question what comes to your mind when you hear the word loop probably is something that repeats itself right and except it's exactly that until now the execution of the code has been sequential we execute everything from top to bottom continuously on our previous lesson we introduced conditional uh execution or control flow which kind of breaks a bit the sequence of execution but sometimes you want to run a cert a certain piece of code multiple times that's when you use loops for example if you want to write a script that adds every single number from 1 to 10 every integer from 1 to 10 you would use a loop to do that and the first type of loop that we will talk about is the while loop basically the idea behind the while loop is that a certain piece or section of your program will be executed while a certain condition is true so here we have the syntax as long as this condition is true the loop will execute the code inside of the loop and when the condition is false the execution of the code will stop let's have an example so what we have here is a variable that we assigned a value one right and as long as this variable is lesser or smaller or equal to 10 what we are going to do is we are going to increment one into this variable and remember that we talked about this this sign here and basically what this means is if the initial value of this variable is one then we're going to add one to that one and it makes it two then we're going to add another one and that's three right to make it even easier for you guys to understand let me run the code and let's see what's the output here we have the output and the first number is two obviously because first we are doing one plus one that's equal to two and we are going to do that until the value of sum is 10 and we are going to add 1 to the 10 which is 11 that's why we have 11 here but this loop will then start running because the new value of sum will be 11 and 11 is not equal or smaller than 10. now there is another type of loops called do while loop and the main difference between a while loop and a do while loop is that the while loop will first check the condition and then execute the code and the dual loop executes the code then checks the condition and here is the syntax do something while the condition is true in terms of example i will use the exact same example here we have a variable with the value 1 and now we want to add that sum until the value of sum is great is smaller or equal to 10. oh sorry here this should be some some two and let me comment this code here so what's going to happen here the output will be the same right will basically be the same output and for this example maybe it's kind of hard to understand the difference and it may look as if do while and while loops do the same thing but that's not true and i will show you that in a bit now let's talk about the difference between a dual and a while loop so i will show you an example where you guys will be able to see the difference so here we have a while loop and what we are doing here is while the value one is smaller than six we want to increment that value and then we want to print the value if you see here if you pay attention the initial value of the variable value one is seven so this loop is not going to execute because seven is greater than six if i run the code we will have here the while result is seven now let's do the same thing but now using a do while loop so we have a second value 2 which is 7 again and here is the do while loop do increment while the value is smaller than 6 and then we want to print the do while result if you guys pay attention here you can see that the value the while result is seven but the do while result is eight why simple because this is not going to execute because this condition is false from the jump from the beginning this condition is false but the do while does not check the condition before executing so it's going to it's going to execute this code once and having this is the same as having plus equal one this is an increment operate operator this is going to be executed and then we are going to check the condition now let's talk about breaking from a loop because sometimes you may need to break from a loop after or when some conditions are met inside of the loop let's create another variable called value 3 right and here we have the loop it does the same thing basically so we want to execute while the value of value 3 is lesser or equal to 10 but in here let's add something so basically we are saying that if the value is 5 we want to break from the loop so we want to exit the loop now if you check the output here if you have a look at the output this is the output we only have two three four five and then we break we break from the loop so basically here the condition was matte so the loop was broken and you use the break keyword to break from a loop or switch statement now let's talk about another type of loops which are somehow more popular than the while loops because normally people use for loops the major difference between a while loop and a for loop is that when you use the for loop you know how many times you want to iterate how many times you want to run the code and when you're using the while loop you don't really know how many times you're going to execute the code you're just going to execute until the condition is met in simple terms that's the difference so the syntax for the for loop is a bit complicated at the beginning but it's very simple once you understand so you have your four and in here inside the parenthesis we have the initialization we have the condition and the action and then the code in here let me give you an example so that you guys can understand so this is how the loop works initialization first we are creating a variable and we're going to initialize the value of that variable to one right condition while the value of one is lesser or equal to 10 we want to execute the this for loop and the action every single time the code is executed inside of the for loop we want to increment the value of i and here we are printing i if i run this code here you can see the output is one two three four five six seven eight nine and ten this is what the slope is going to do if for example i remove this now we are only going to have nine numbers because i must be smaller than 10 and here we have one two nine now the continue keyword sometimes you may need to skip some iterations inside of a loop you don't want to break the whole loop you just want to skip a particular iteration for example if you want to display every single number from 1 to 10 but you only want to display let's say even numbers you would do something like this so here we have the for loop then we have an if statement here actually this is the wrong sign we want to find the reminder it's not a normal division okay now let's run the code so as you can see here we only have odd numbers because when you divide even number with two the reminder is zero and to get the reminder this is the operator that we use the percent the percentage sign now one thing that i want you guys to know is that there are another types of for loops for example for in and for each loops but for me to talk about those loops i would have to have talked about other concepts such as collections and classes and objects so we are going to talk about those in future classes or future lessons hello beautiful people welcome back to the course on this lesson we will have an introduction to functions the official definition or if you search for functions you will see something like these functions are self-contained modules of code that accomplish a specific task right maybe understanding this is a bit different difficult so let me explain you with an example let's imagine you want to write expressions to add numbers so you would do something like this right you have the first expression that adds 1 plus one and then if you want to add a different set of numbers you would write a very similar expression just changing the values here you will add 23 and 21 and you will add 30 and i mean 90 and 1. what's the problem here is that you're basically repeating the exact same code the logic is the same right and let's imagine you want to add a thousand different numbers you're not going to write this a thousand times so that brings us to this concept called dry right and dry basically stands for don't repeat yourself you have to avoid code repetition if you have multiple pieces of code that do the same thing you probably can just create a function out of that code and call that function so here's the basic workflow of a function function basically takes an input does something to it and produces an output okay that we may have some iterations on this workflow but the basic workflow is like this we have an input we do something we have an output for this example we would have the numbers as the input the function would add those numbers and then would return the result here is the syntax of a function the basic syntax of a function is like this we have a return type the function name the inputs or parameters that are passed inside of this parenthesis and inside the curly braces we have the function logic and then we will return a value right the return type can be any data type can be an integer a string a double or can be empty what's called avoid right but you'll understand about that in a minute with more examples so the first example here will be a multi-parameters function with a return type going back to the example that we have about right here we we want a function that's going to add two values that's why we are passing those values here inside of this parenthesis and this are called parameters when you define parameters you usually have the data type of the parameter and the name of that parameter here inside of the parenthesis now the operation that we want this function to do is we will create a local variable and operation is addition so we want to add the first value to the second value after that is done we want to return result if you pay attention result is of type int and this is the type that we are returning for this function right so instead of writing this every single time we want to add two numbers we can define a function just like this following this structure and execute this code every single time we want to add numbers now another example of a function is the main function and you already know what the main function does but again the goal or the objective of the main function is to start the execution of the program and the structure of the main function is like this it has no return type it doesn't return nothing that's why we have void here usually when functions do not return a value we have void right main is the name of the function it doesn't have any parameters we don't need parameters for this specific example that's why the parentheses are empty and here inside of the curly braces we can insert the code or the logic of the function so in here we will have another local variable called result which is going to be equal to add1 comma two these are called arguments right and uh these words parameters and arguments are misused most times but usually arguments are the values that we pass to the function and parameters here is the definition of those values and this is how you called a function function name then the parenthesis and you pass the arguments for that function now if we print result is here what's going to happen is we are going to add these two numbers assign the value to this variable and print the result here and here is the output result is 3. now let's talk about a different type of functions with a single parameter and no return type let's suppose we have a function called is even or odd and basically what it does is it checks if a value is even or an odd value to do that the logic is like this we will do value modulus 2 and if the reminder of that of this operation is 0 that means that the value is even and if the reminder is not 0 that means that the value is odd so for this example we would have here if this is zero values even if this is zero value is odd and again as you can see this func this function doesn't return a value it operates everything inside of the function that's why we have here void now inside of the main function we call the function like this is even or odd and we pass an argument of 3. if we run this code we have the tree is odd which is true let's have a different number two for example and two is even which is true as well now let's talk about optional parameters right because what happens here is if you try to run any of these functions without providing a parameter let's suppose we have this like this you'll have an error and the error says one positional argument expected but zero found basically when we defined this function we have said that we must have an argument here a parameter here and we are passing no arguments that's why we have the error now let's talk about optional parameters so on this case we have a function called enter name we require the first name and a last name right so what happens here is that when we require an optional parameter we will have the parameter inside of these brackets here and every single time we mix different parameters for example this is called positional parameter and an optional the positional parameter must come first and then we have the optional so here we are checking for the following if the last name value is null we will return only the first name if the value of last name is not now we will return the first name and the last name coming back here to our example let's write print enter name and the name here is santos so as you can see here the value is assigned to xy because the optional parameter is null if we insert santus and enoch here has the last name the output will be different and the reason why this parameter here is called positional let's have a look here at the beginning this oppositional parameters because these values will be assigned based on the position of the argument right because here one is the first number so this will will be assigned to the parameter on this position and this will be assigned to the parameter on the other position if i run this code here you can see that we have santos here because we didn't provide the last name but here you have sanchez are not because we have the last name now let's talk about default values sometimes you may need to have some defaults some values some parameters as default values for example if the user doesn't insert nothing on that specific parameter we have a value by default we have this function here looks complicated but it's simple it's called enter profile details right and usually when you write functions the function name tells the developer what the function does that's why the name sometimes are kind of long anyways for the profile detail we have of the first parameter here called name and we have an optional parameter called position basically is the position that that user has in terms of profession so here the position by default is going to be equal to student so if this field is not inserted or if we have nothing for this field it will default to student let's call this function here inside of the main function now if you have a look here at the output we have santosh position student right even though we didn't provide nothing here now let's try to assign a value to that parameter so let's pass an argument here called developer and if i run this code the output now is different as you guys can see now we have santos name position developer now let's talk about named parameters you see the thing with positional parameters is that when you insert values here when you pass arguments you don't really know where these arguments will be assigned to unless you know the execution of the function unless you know how the function is defined right but sometimes you want to have the ability to assign each value here to a different parameter regardless of the position for example we have here a function named edit profile and then we have the new name and new password parameters when the parameters are named parameters inside of the parentheses we have curly braces and the named parameter must be defined inside of the curly braces and then we are going to print new name we have here the new name and new password is this now coming to the function call you have here edit password new name is equal to santos a new password is equal to one two three it doesn't matter if you start here with new password is equal to one two three abc and your name is equal to santos the execution will be the same if we run this code you can see that we have here the output new name santo's new password one two three now let's talk about mixed parameters again for the second example we have a positional parameter which is a name and a named parameter called position and what's nice about named parameters is that they are optional parameters as well so even if you don't pass nothing here the code will be executed with no problem right and every time you have mixed parameters the first parameters must be positional parameters and afterwards you have you can pass the optional or named parameters so the example here is user details santos and this will be assigned to the name and the position is software engineer let me run this code and as you can see here is the output and it says name santos and position software engineer now some tips on how to write better functions one rule says maximize the use of pure functions and minimize side effects a side effect basically is when you do something and for you doing that you get something else sometimes not intended right for example you get sick you start taking medicines but then you get fat that was a side effect of taking that medicine right so in terms of functions if you have a function that is going to alter the behavior of something else outside of this outside of the function that's called a side effect for example we have this function here here called print hello world right this function calls another function called print and as you can see print is a function this function will display the message to the console right this is a side effect of calling this function because we execute another function that's going to display something to the console another example to make sure you understand the point let's imagine we have age here a variable called age and then we have a function called void display name right the goal of this function is to display a name but the first thing we do is we alter the value of this variable here this was not intended to happen so this is a side effect sometimes we may need to have some side effects in the function right so i want you guys to understand that and then we are printing the name right what you want to do is you want to minimize the use of this type of functions that will alter things outside of the function now functions with with no side effect are called pure function for example hello world what this function does it returns a text hello world and it doesn't alter nothing outside of this function that's why it's called a pure function the only task of this function is to return hello world another example is username which does the same thing we are passing a parameter called name and it returns that parameter so there is a principle called single reusability principle or do only one thing and this basically states that every single function that you write must be intended to do one and only one task right if your function does more than one thing it does three two four multiple things that not that's not a good practice it's better to have a lot of sub functions that do a specific task and call those instead of having one large function that does a lot of things because it's going to be harder to troubleshoot that function so that's called srp single reasonability principle choosing good naming now when it comes to naming functions you have some principles to follow this is not required but it's advised right first of all the function must describe what it does second point when you have functions with no side effect you usually use a noun noun phrase for example on this case this function just returns hello world so instead of having get hello world or print hello world the name of the function is hello world the name of this function is username right but if your function has a side effect you usually start the function with a verb for example this function prints hello world that's why we have here print hello world this function displays the username and the first thing that that we have here is display the verb right so these are some of the tips on how to write good functions sometimes you are going to execute your function only on a specific section of the code and on those cases there's no need of defining the function to call it only once on those cases again you use something called anonymous functions anonymous functions are functions that can be used without being defined previously let's have an example starting with the syntax of an anonymous function you can notice here that the anonymous function doesn't have a return type or a name and if you return something here the return type of the function will be defined by dart using type inference anonymous functions are called first class citizens that basically means that functions can be assigned to variables can be passed has parameters of other functions and can be returned by other functions as well let's have some examples here let's see how you would assign a function to a variable and this is how function is a data type as well so you can create a variable of the type function and assign this anonymous function here and what this variable will do basically will have assigned to it an anonymous function that adds two numbers if you try to do this with a regular function like this a named function and you'll see that there's an error because this cannot be done you cannot assign this function to an integer now here inside of the main function let's have an example of how you would use that this is a variable type function but when you call it you call as you would do on a normal function you just pass two parameters the numbers to be added and print the result and if you execute this code the output here will be 3 which is the value of the result of 1 plus 2. another use of anonymous functions is to pass them as arguments of normal functions and this is how you would do that let's suppose we have a function called do something and this function tab takes another function has its parameter so the do something function will take this function as a parameter and will call the function inside of the body this is how you would execute this first we call the do function inside of the main function and here is how we pass the callback function which is an anonymous function because you can see we do not have the name of the function here we just have the body of the function so what this function will do what the callback function will do is hello there this is what the function will return a string saying hello there if we execute this code you would get hello there as the output why because this was called in here inside of the print statement and since the callback function returns a string this is what's going to be displayed here and now let's have another example here on how to return a function now on this example we will build a function that multiplies numbers right but the difference between this and a normal multiplication is that here first we will select the multiplier and every single value will be multiplied with that multiplier if you didn't understand no problem just stick with me here so first we have a function called apply multiplier that returns another function and the function that we are going to return here is an anonymous function that takes a value and what happens this function returns the result of the multiplication of this value and the defined multiplier here i know this looks complex but let's have an example on our case we want to triple every single value that we passed to this function so we will define a variable called triple and then the apply multiplier value will be 3 because we want to triple inside of the print statement we can have the triple of 4 is triple and remember that triple is a variable of the type function that's why it can take parameters and the parameter we are going to pass is 4 and if you check here on the function definition we will take the value of the parameter and multiply that with the multiplier this is the output we get the triple of 4 is 12 which is correct now another use of anonymous functions is with for each loops and as you guys remember i believe that you guys remember i told you that we would talk about for each loops in the future and this is the future let's suppose that we have a list and this is how you define a list we have a list of numbers right two three four and five now what we want to do is for each value of this list we want to do some operations here so what we do in here is for each value we want to run this anonymous function right and what the function does it's going to double the value that we have here it's going to double every single one of these values here so the anonymous function is going to print double of value is value times two and if i execute this code you can see the output here the double of two is four three is six four is eight and five is ten by now i believe that you guys already noticed that every time we define a function we have the code to be executed inside curly braces but you can write functions in different ways in dart and another way of writing your functions is using arrow notation right and this is somehow a simpler way and a cleaner way of writing your functions in dart for example let me start with a function a normal function that's not an error function and we have this function here that will add two numbers and return the value by now your value we are very familiar with this type of functions now let's convert this to an arrow function and this is how you would represent the same function right there's no need of writing a return here automatically this will be returned and there's no need for curly braces on this example and this function takes one two three lines to be defined and we defined this one in a in in a single line let's have another example we had this function on our previous example apply multiplier and let's convert this to an error function first we would have curly braces and then we would return this expression like this and as you guys can see it's simpler to write this function like this rather than having it like this hello beautiful people welcome to object-oriented programming in dart this is just an introductory video and here we will talk about the basic concepts of object-oriented programming as per the definition object-oriented programming or oop or oop is a specific way of designing a program it allows us to think of data in our programs in terms of real world objects with both properties and behaviors for example let's suppose you are designing a school a school management system and you would have to deal with data related to the students how would you store that data the building data types of dart and many other programming languages only allows us to store data such as integers strings and booleans but not complex data types such as students on such cases you would want to create your own data type give that data type you your properties and define some behaviors for that specific data type right so that's basically object oriented programming on that case the object would be the student let's continue and you will understand everything in a bit so one of the core concepts of object-oriented programming is called a class so what's a class you ask in simple terms a class is a blueprint of an object the official definition says something like a class is a user-defined data type or is a user-defined type that describes what certain type of object will contain and how it will behave so if a class is a blueprint of an object what's an object object is an encapsulation of data along with functions to act upon that data or object is an instance of a class to make everything even more understandable for you guys if a class is a blueprint of a house the house is the object or the object would be the house on that example right let's have some code and you will understand this perfectly in couple minutes defining a class how do you define a class here we have a class called car right and to define a class you first have the keyword class to tell the compiler that you are defining a class class name and then you have curly braces inside of the curly braces we will define class properties which are these variables here and class methods which are functions of that class on this example we have card which is the class right and the class card has two properties make and body type make is basically the brand that makes the car and body type is the body type of the vehicle so this is the blueprint let's create an object so to create an object of a class you just write here var car is equal to car car is the name of the object and this could be anything and here you don't have to write var this can be a constant or you can ride car car just like this you can specify the type of object that you are dealing with here the type of class and as we defined this is a user defined data type so how do you change properties of an object on this case our car the make or the model of the car will be equal to ford and the body type of the car will be suv right because here everything is empty and here we define the properties for that object now let's print the object and see what happens now if we print the object the output will be instance of car why because the compiler doesn't know how to display the properties of the object yet the only thing it does know is that this object car is an instance of the class car right and you have to pay attention that the object car is lowercase and car the class starts with a capital letter now let's suppose you want to display the data related to the car how would you do that we would have to override a method as i mentioned before every single thing in dart is an object so even the class car is a subclass of the class object right that would be a bit hard to understand now because we still we are still introducing the concept of classes right but a class can have a subclass or child's okay and uh on that main class or the parent class object there's a method called to string right we can override the method to string to display something else that it's not instance of a car let's have an example now here inside of the class we start with the override keyword to tell the compiler and other developers that this method doesn't belong to this specific class it belongs to a parent class and we want to overwrite the method the method that we want to override is a method called tool string and you already saw me working with the tostring method now what we are going to do here for the tostring method we want to return a string and the string says car we have make where we are defining the brand of the car and the body type of the car right now if we print this again what's going to happen now we have here the output says card name forward and body type is suv right previously the method to string would return the instance that the object belongs to or the class that the object belongs to or the class that the object is instantiating right but when we override this method we are basically changing what's going to be displayed when we try to print that object and i can even change this to say something like and if i print this we will have here this is a car and what this means is when we create a user-defined type with a class our program will be able to understand all of this but if we try to send this data to a database for example our database doesn't know what a car type is and doesn't know how to store or to trade that data right on such cases we want to convert the data present on the object car to something that a database can understand so we would convert this to a string because databases know how to deal with strings now if you pay attention to this example here we already did that we already converted this instance to a string but there are rules widely used to serialize the data and usually you want to convert your object to a json and json stands for javascript object notation probably every single database and apis will understand how to deal with this data how do you do that first let's create a method a method is basically a function that belongs to a class so this is called a mate a method and the method is called to json and what it does is it returns a string with this strange pattern here right this is a json object json objects start with curly braces and the properties are defined inside of double quotation the value and the keywords the value of the property and the property are separated with a column right and every single value or key value pair is separated with a comma just like this it may look a bit complex now but you will understand you will understand it even better when we write more examples about this and this is very similar to a data type in dart called map but we are going to talk about maps in the future so basically this is how you serialize your objects to json notation now if you want to print the data you would just have here car.json this is how you access methods inside of objects and you already seen this by now right but since we want to run this method that belongs to this object we write the object name dot the method name and method is basically a function let's run the code and see what happens so as you guys can see here the output is basically the same but this since it has the json format that would be very easy to send this data to a database to an external database now let's talk about the cascade notation let's have an example if we want to change the data of the car without using a cascade notation we would do something like this we would have car.make assign a value to the property car dot body type and assign a value to that property as well if i print this you can see that the data changed here because we changed the name of the make and the body type property as well right but sometimes a class may have a lot of properties and writing or reassigning values like this may be time consuming on those cases you can use the cascade notation and you would do that like this and you would have the object name the class name and you would have these two periods so these two dots and then the name of the property you wish to change you can do this for as many properties as possible but the last property it has to be followed by a semicolon just like this and if i print this code i would have something like this i just don't know if you guys are able to see this but the data changed again here now i have nissan for the make and suv on the body type so this was basically it this was the introduction to object oriented programming in dart hopefully everything was clear but if you didn't understand the point feel free to ask i'll be more than willing to help you see you guys on the next one hello beautiful people welcome back to the course on today's lesson we will be talking about constructors now a brief definition constructors are methods that create or construct instances or objects of a class right we talked about method methods on the previous lesson and we have here some examples basically are functions that depend that belong to specific class now constructors as kind of a special type of methods because first they always have the same name as the name of the class and the return type of a constructor is an instance of that particular class now why would we need constructors for example if we want we want to initialize an object of a class with a specific value or with specific properties we would use constructors on that case let's start by talking about something called default constructor by default every single class has a constructor called the default constructor and it's something like this it looks like a function without a body and you don't have to explicitly write a default constructor because dart already understand that every class comes with a default constructor and let's start talking about long-form generative constructors generative constructors can have parameters that are used to initialize properties of that class and we have long form generative constructors that are more or less like this so what we have here is a method with the same name as the class a constructor it takes two parameters one called make another called body type and it initializes the values of the make and body on the class this values here it initialize them with these values here right we have here that the keyword this in the constructor body allows to to allows you to distinguish which variable you're talking about for example here we have make which is a property of this class and we have make which is a parameter of the constructor when we use this basically we are saying that we want to initialize this make here which is a property of the class with make the the parameter of the constructor let's have an example so now we can call this class the same way we would do with a function and we pass here the default properties for the car make and the body type if i run this code you can see here that the make is toyota and the body type is suv the short form of the same constructor would be like this and just see how much cleaner this code here writing this is the same as writing all of this here so the short form of the constructor does it doesn't have a body right and the properties are like this this dot make and this dot body automatically dart will know that the argument that we pass here will be assigned to this make to this variable here to this property and the argument passed here will be assigned to the other property if you run the code the output will be the same now these are called an unnamed constructors we have another type of constructors called named constructors and the syntax of a named constructor is like this class name dot identifier name right and what's the use of named constructors for example you may need or you may want to create a specific type of object and for that specific type of object you want to initialize the properties of the class with a specific values by by default with specific values by default for example we have here car dot trucks and here we are initializing the data on this case it doesn't matter the name but we want the body type of this class of these classes instances to be truck let's write an example and on this example we initialize this object my card with the named constructor if we run this code this will be the output as you can see we do not have the name of the brand but we have the body type which is trucks just to make sure that you guys understand this concept i will have another example another named constructors another named constructor called sedan and the example would be the same now if we run the code you can see that all of these objects are instances of the car class but they have different values here for the body type of the cartoon is sedan and the first one is a truck so this is an example of named constructors now what's happening here we are going against one of the principles called don't repeat yourself the problem here is that these values are initialized on the unnamed constructor this one but we are initializing the values again on these named constructors right and these may become a problem when we have a lot of constructors doing different things on the same data to avoid repetition there is something called forwarding constructors or redirecting constructors on this case this is a named constructor but what it does is that it calls the unnamed constructor the default constructor here and it assigns the value like that right let's have an example of this case here now on the surface it may look like nothing changed but writing this like this is way better than doing this like that because the values are not initialized inside of this constructor but they are in fact initialized using this constructor here so i would advise you guys to follow this approach here rather than this approach here let's talk about named parameters right if you guys remember when we were talking about functions we talked about positional parameters and named parameters and that's the same thing with the constructors an example of using named parameter would be something like this now you're going to have an error because you already have defined this here and you cannot have two unnamed constructors so now that we removed the other constructor we would have something like this and if we initialize an object for that class we would have something like this and if you have a look at the example you can see that this is very similar to how we call functions right but basically now we have named parameters on our constructors welcome back to the course on this video we will talk about initializer lists and let's have this example hopefully you guys are able to understand this because there is nothing new on the screen but anyways here we have a class with the following properties id name and account balance and then we have a constructor that's going to initialize this this properties and we have overridden the two tostring method if i execute this code so this is basically what we have but one of the problems with this approach is that someone could just do something like this and as you guys can see here i just i just changed the account balance from 200 to 4000 and if you had something like this on a real app this would be a very big problem there are a couple ways to avoid this first of all there is something called private variables we can create fields that are only visible from inside of the class and to make a variable private you just have to precede the name of the variable with an underscore now you have two options here you can directly initialize the value of these variables or you could do something else now as you can see here the names that we're passing through the parameter are not the same as the class properties and if someone tries to access this externally following the names passed to the parameters nothing is going to happen because these are these are not properties of this class have an error here because these variables are not private so you have to change the name and proceed them with an underscore and as you guys can see it's not possible to just change the value like this and just like this we are able to initialize these private variables here with the names from the parameters and what's good about initializer lists is that this is ran before this object is created so now if i try to do something like you could see that now we have a problem because this field does not exist but for example if i do something like this we are still able to get the client balance amount right so what's good here is that you are unable to change this value here you cannot change it but you can still get the the the value and this may be a problem and it kind of goes against the whole idea of using private variables right let's suppose we want these variables to be hidden so that the user is not able to change or to access the variables these variables here are only accessible because they are private yes but the main file and the class are the main function sorry the main function and the class client are both in the same file now to make this unaccessible we would have to remove this class from this file to another now we have that file there how do we use or how do we access the code present in this file on the other file we use a key a keyword called import and then the file name what you want to do here is import then the file path but when the two files or when the files you you're trying to access are in the same folder has the file accessing them you just write import and file name now as i was telling you guys now we have this error when we try to access the client balance we have the getter isn't defined right because these variables are private now we are going to talk about getters and setters very soon but this is how you make variables private right and the only way to access those values for example would be to run this code because the tostring method returns the values but you cannot access a specific field now let's have another example for example here i've set the balance of the client to be -100 if i run this code nothing is going to happen and here you have the balance but if you think about it it's not super logic to create an account with a negative balance right so how do we ensure that some conditions are met we use the assert keyword for example on this case let's imagine that we want to make sure that the balance is never below zero so we want to assert that the balance is greater or equal to zero let me execute this code and what happens we have an error the app doesn't run and it specifies where we are having the error balance is greater or equal to zero if we go back to the example and change this to zero and run the code everything works as intended welcome back to the course on this lesson we will talk about constant constructors here we have an example that's very similar to what we had on the previous lesson right basically we have a class with the constructor and an override method right and if i run this code this is the output here as we discussed on the previous video even though there's some level of privacy here because the class and the main function are both here on the same file we would be able to do something like this so as you guys can see even though this variable or this field is private we were able to change the value of that field right now to avoid class properties to be changed we can use final the final keyword what this will do it will make sure that the value of these fields are only assigned once that's why we have the error here and what's even better about this is that these fields don't even have to be private no more now the variables here are public but they are final what that means is if we create an object here we won't be able to change the properties of that object for example if we try to change the brand of the smartphone object after it's created we will get this error here because this is the final field what's the difference between having this constructor here and a normal one the difference is this the difference is this for dart all of these three objects that we created here are the same object dart will not generate or will not allocate new spaces to store these objects because it will understand that this object is equal to this object which is equal to this object so in terms of performance is always better when possible to create the constructors of your classes constant constructors on this lesson we will talk about factory constructors now up until this point every single one of the constructors that we have talked about are called generative constructors that basically means that these constructors only return a new instance of the class on which they are defined right but there there is another type of constructors called factory constructor and a factory constructor can return an instance of a class that already exists or can return a subclass of a class right we haven't talked about inheritance and subcla subclasses now so that may be a bit hard for you to understand but don't worry about it a small example of a factory constructor would be something like this so we start with the keyword factory and then here i have the name of a named constructor and the implementation inside will be something like this body type will be suv and if i run this code the output will be suv right so this is a introduction to factory constructors and we will work and use factory constructor from here on right i just wanted to kind of give you guys a very brief overview of what a factory constructor is hello beautiful people welcome back to the course on this video we will talk about getters and setters right now inside of this folder we have two files the main one and one called student so here we have some private variables the id marks of the student and the name of the student here inside of the main file we can create an object of the type student first of all we need to import student and just like that we have created a new student object an instance of the student class right but there's a problem here since every single variable here or field of this class is private there's not much we can do from the main function right we are unable to access the student's name or the id or the marks of the student right so if you want to be able to get the value of private properties outside of the class we use something called getters and for this example we would have something like this and here is how and this is how you define getters right first we start with the return type of the particular field id the getter name and the value that it's going to return if i go back to the main file and do something like now we can access the fields and as you can see here student name is santosh now you don't have to write the exact same field name as the getter for example here i can change this to username and i would have to change the value here to username but since we already defined that the getterusername returns the value of name the output will be the same another thing that i want you guys to know is that the number of getters do not have to match the number of properties here for example let's create a new getter called is student smart so what's going to happen here this getter will define or will classify the student as smart if the marks of the student are over 80. we have here student name is him smart is true right so this are getters sometimes you may want to change the value of private variables because if you come back here and you try to change this for example you really can't do much because you cannot change the value of getters all they do is get the value on such cases we use something else called setters so here we have defined a setter and how you do that is you start with the set keyword then the name of the setter the parameters it's going to take and the values that are going to be changed coming back to the main function we would have something like this new name of the student is mario and as you can see here student name is mario now so we were able to change the value of a private variable using setters and we were able to get the value of private variables using getters so this was it an introduction to getters and setters welcome back to the course on this video we will talk about static members up until this point if we were to access properties of a class we would have to define an object which is an instance of that class and then access the property for example if you want to access the college property for this class we would have to create an object of the type student and then access the property using the object but let's imagine we have a case like this where we have a student class and we have college but college is common for all of the students so we want to make college a variable that belongs to the class and not a specific object and every single object that is created from this class will have this variable here in common to do that we use the keyword static now what's going to happen here first of all is that because this variable now belongs to the class you could do something like this now to access this variable the college variable we don't have to create an object we can access it directly by just using the class name and the college variable name let's have another example with this method called display default values we can see that the college is bangalore university so when you use static methods and static variables you do not have to create instances of the class to access the members of that class the static members of the class right and this is super powerful to use and you will understand once i give you an example of a pattern called called singleton which is a design pattern we will talk about that on the next lesson welcome back to the course on this video we will talk about the singleton pattern the singleton the singleton pattern is one of the most popular design patterns and um it's super simple to understand a singleton pattern or a singleton class is a class that can only have one single instance right and this is extremely useful for example if you are creating a class for a database that's going to work on your app you only want one single instance of that database you do not want to initialize the database when you create a new object of that database class in the case of classes such as user obviously you would want to work with normal classes but classes such as databases and other examples you usually want to go with singleton classes and how do you create a singleton class now on this first line we created a private named constructor right and this underscore will make it so that this class cannot be initialized normally so you cannot do this because it will say that the class doesn't have a default constructor so you cannot initialize this class like this now if you create a static static member of the type database instance that returns an instance of this class you could do something like this then now this is a singleton pattern super super simple what happens here is that even if you do this all of these objects here actually point to the same reference of this class so if you do this the class is not going to be in initialized or instance instantiated again and a cleaner way even a cleaner way even much more cleaner way of doing this would be to use factories and another advantage of using factories is that you can hide the internal implementation of a class outside of it for users using your class calling or instantiating this object like this it looks like a normal class but this is a singleton class and what's better is that if you decide to change this from a singleton class to a normal class you wouldn't have to change the implementation here so this was basically it a very brief introduction or overview of the singleton pattern using static members welcome back to the course on this video we will talk about nullability and sound null safety that's a very popular word or term to hair when people talk about dart 2 and flutter but what sound null safety in simple terms null safety basically means that dart will not allow you to assign the null object to variables and what's now again in very simple terms null basically means no value for example for you to start with this first example go to your pop spec file and make sure that the version of dart here is 2.10 if it's 12 or onwards this will not work so change it to 10 and you will change it back very soon now coming back to the file that we want to add it let's suppose we are creating user accounts and we have a field called phone number and this is the user phone number for this example so if the user does have a phone number this is the phone number right if the user doesn't have a phone number maybe we would have a default value for users without a phone number let's say zero right but this is not a very good practice first of all because you would have to let other developers know that zero means no phone number and you would run into a bunch of issues doing this this way on previous versions of dart you could just do this now even though vs code will tell you that this is not a good thing to do but you could do it no problem and this thing basically means that the phone number filled has no value let's see what's the problem with doing this now let's create a function that checks if an integer is an odd number or even number and here we have a very basic function that checks if an integer is even or odd if i run this code the output will be 2 is even which is true if i change this to 1 1 is odd but if i change this to now no error apparently but let's run the code boom now we have an error and the error says no such method is even called on now and this is a very common problem this is a very common problem we as developers sometimes we assign null to variables by mistake and this makes our code to break and what's worse is that you can only know about such type of errors on runtime basically the application will break while it's running and this is a very big problem now from dart 2.12 onwards let's change here the version what happens is that is no longer possible to assign now to variables like this and the error says a value of the type now can be assigned a variable of the type int and this will uh will prevent our code from breaking while it's running now after i changed my dart sdk version from 2.10 to 2.12 something happened a few errors here and now to explain to explain to you what's happening i will introduce a new concept called nullable types and non-nullable types what that means basically is that the default types in dart ain't string bull cannot be now on cases such as this if you want a variable to be null at times we have to follow the data type with a question mark and what this thing means basically it's saying that this value here may be now but of course the user the user id cannot can never be now so instead of doing this we can set a default value here and here we are so in summary null safety basically means that non-nullable types cannot be assigned the null object the null value right and if you want to assign or if you want a variable to be able to take null has one of its values you have to follow the data type with a question mark that basically means that this variable may be null let's talk about how to handle null values and type promotion for example i have a variable here called name that may be null and i try to print the length of that variable or the length of the string name something like this and normally what this method would do is basically tell you how many characters are in the string name but since this value may be now this is not going to run and this is very good because now this is a compiled error not not a runtime error and you are able to troubleshoot this issue even before you run the code now let's talk about type promotion the dart analyzer is super smart even if you just do this you can see that now the error goes away why because the dart analyzer knows that this value can never be now because we assign a value before we call length and if i run this code you can see that the output here is six and six is the number of characters that we have on disney now you may be asking yourself how this type promotion happens or what it means basically when you assign a value to a nullable nullable variable the type of the variable changes from nullable to non-nullable so dart knows that it can use the value or you can access this variable with no concerns because this is a non-nullable variable it was promoted from a nullable variable to a non-nullable variable hello beautiful people welcome back to the course now continuing on the topic of dealing with null values on this video we will talk about null aware operators and the first one it's called if null and it's represented by two question marks and how it works is like this let's say we let's say we have a variable a nullable variable of the type string called name since this this variable can hold null has its value if you want to assign this variable to a non-nullable type for example username what we would do is name two question marks default name what this means basically is that if name is null we will assign default name string to the username and the output says default name and if i were to change this now we have an error because this value here will never be null because we already assigned a value here now let's talk about null aware assignment and it's represented by two question marks and an assignment operator or equal sign let's suppose we have here double price which is nullable and if we follow the previous example we would have to do something like this if price is null the value here that will be assigned to amount but with null aware assignment we would do something like this and what this means basically it means that if price is null we will assign 0 to the price variable and we could just do this and now we have here that amount to pay is zero now let's talk about null hour access operator and it's represented by a question mark and a dot if we do something like this and we have this nullable variable called name if we try to access the length of the string and we use dot length to get the number of characters present on the string we are going to have an error right because we cannot call length here because the value of now of name can be now right on such cases we would do something like this question mark dot basically here we are saying that we are aware that this value may be now and if we run this code the output here is now because the value of name is now now let's talk about null ascertation and we use an exclamation mark here and this is not a not operator let's see this example we have int age which is a nullable variable and we are assigning age to my age basically having this exclamation mark here we are saying that age will never be null so we are telling the compiler or we are telling dart to assign the value of age to my age and we are we are asserting that age will never be now on this example this is not the right thing to do because age may be null but this is how you do now let's imagine we have a class called user with two properties a name and an id and we will use null aware cascade operator if we create an object of the type user here that can be now this is how we would assign values to that so this was basically it a very simple to understand video and see you guys on the next one hello beautiful people welcome back to the course on this lesson we will have an introduction to collections in dart and in very simple terms collections are basically data types that can store more than one value basically data types that can store multiple values and the first one that we will talk about is list or lists you have seen lists because we had some examples on previous lessons where we where we had to use lists but here we will have a deep dive into lists so what's a list a list is an ordered group of objects simple as that now how do you create a list usually in dart you have a lot of ways of accomplishing the same goal but one of the most common ones is this variable list name and then you have these brackets here and the list elements go inside of the brackets and they are separated by comma commas and this can store any type of data types it doesn't have to be integers it doesn't have to be strings only can be integers it can be bool and even custom types like user defined classes another way is doing this numbers int and what this crazy looking syntax means basically here we are we are we are telling dart that we want to store integers into this list so when you want to define the type of elements that you're going to store inside of a list you use this generic types and again here it can be int can be a user defined class any data type is valid now if we do something like printing runtime type and if you do this variable name dot runtime type what dart will do is it's going to return the type of that variable let's do that for both lists and run the code as you guys can see here the output is list string and list int basically this is a list of strings and this is a list of integers now how do you print a list super simple just like this you can have print and then the list name and if you run the code the output will be all of the elements of that list on our case dart python java and go and how do you access items in a list if you come back to the definition pay attention to this part where it says ordered group so a list has a specific order every single element inside of a list has an index which is basically an address right and list start with the address or the index zero so dart here is at the position zero python is at the position one java at the position two and go at the position three now to access items in a list you could do something like this element programming language then inside of these brackets you have the index number on this case we want to access dart so you write the list name then you open these brackets and the index of the element and now we can print the first item of languages list is and if i run this code we have here the first item of the languages list is start which is correct if you do something like 2 we would have the third item which is java now how do you get the index of an item to get the index of an item list lists come with very handy methods and one of them is index off so what you do basically list name dot index of and then here you enter the element that you're trying to find the index for so on this case we will have the element at the index is programming languages you open brackets index remember the index is going to be an integer because we will return the index here and we are going to access it here if i run this code the output will be the element at 2 is java now how to assign values to list elements on this case our first element is dark but we can change that from dart something else on this example let's change it from dart to javascript and if we print the list you guys can see that the first list here is dart python java and go but here is javascript python java and go now let's imagine we want to find the index full dart and remember that dart doesn't exist no more because we replaced dart with javascript and if we try to print the index of dart is here we have the index of dart is -1 and basically what this means every time you get -1 has the index of an item in dart that means that the item is not present in the list now how do you add elements to a list just type the list name dot add and the element you want to add now if we print this list we will see that at the very end of the list now we have dart because we just added the element here to access list properties or operations you just have to type the name of the list then dot that's how you access methods because list is a class and it has methods how do you remove elements obviously list name dot remove and then the element that you want to remove if we run this you will see that now this list doesn't have javascript another way of removing items from a list is using the index so we would do something like this list name dot remove add and then the index from this list we can see that the second index is is the third element on this case is go and if i run this code you can see that go is no longer present in this list now let's talk let's talk about nullable multiple and immutable lists if you have something like this you may think that because this list is final this is not mutable but unfortunately this is not true what happens here is that i cannot reassign this variable name to an empty list for example if i try to do this we will have an error so we cannot change the list itself but we can change the list elements what i mean by that is the following if i remove the element at index 1 and add next jazz to this framework list you guys can see that i was able to edit the content of this list so with final you'll be unable to change the whole list but you can change the elements present inside of the list if you want to make the list immutable you have two ways of doing that one of them is using list dot and modifiable and then you pass the list that you want to be unmodifiable on this particular example if we try to do ides dot add value seems like nothing happens right but if you run this code this will give you a compile time error i mean a runtime error boom so the bad side about using unmodifiable lists is that they only return runtime errors so you will find the error when you run the code now another useful list methods or list operations are the following if you want to check the first element of a list you can do something like this list name dot first and here we have python because this is the first element of the list and obviously if you want to check the last you will do list name dot last if you want to check if a list is empty you could do something like this list name dot is not empty or list name dot is empty and this is going to return a boolean for this example we have here does language has have elements and the result is true because programming languages list in fact does have elements now how to loop over a list do you remember when we were talking about loops i said there are two types of loops one called for each and another called for in that would be very useful when we start talking about lists let's talk about those for in loop is super simple so you have here four language in programming languages basically for an element in a list on this case we want to do what we want to print that language so for every single language present inside of the programming languages list we want to print the language if i run the code the output will be python java and dart another way of doing this is for each loop and we already talked about this so for each element in the programming languages loop we want to call this anonymous function that is going to print the element name the output will be the same we have python java and dart there's something super interesting called tear off basically it tears off unnecessary syntax because the output of this is going to be a string and the input of the print function is a string as well we could do something like because we know that for this case specif for this case specifically before each loop returns a string and the print takes a string has the input so you can just have for each and then print that's a super cool thing that dart does and if you run this code the output will be the same now let's talk about this thread operator let's imagine that we have a list called development tools here we have computer and then we have this three dots then programming languages the entry dots frameworks what's happening here basically this operator here will take all of the elements of the programming languages list and we'll add them or we'll insert them into the into the development tools list and do the same thing for the frameworks the output will be something like this computer python java dart flutter fast and next gs there's something called a null spread this is when you are dealing with nullable lists so for this example we have a list called first list that can be now and we have a second list that it's going to and we have a second list that is going to be equal to 1 which is the first element and we have the spread operator with the question mark because this can be null and if the value here is now this section will be ignored the output will be one now let's talk about collection if collection if is super cool what it does is it allows us to check certain conditions before adding elements into a list for example let's imagine i have a list called my favorite languages and i have here dart and python by default and then i want to check if english is a member of the programming languages i want to add english what's going to happen is since english is not a member of the programming languages list this is not going to be added and if i run this code the output will be dart and python let's imagine we have something like java and the output now changed now we have java because java in fact is a member of the programming languages list now let's talk about collection 4 which is super similar to collection if what it does is that it allows us to iterate through a list and do some operations before adding those elements into a new list for example we have here uppercase framework and this list contains the same elements as the frameworks list the only difference is that all of the elements here are uppercase uppercase elements the output would be flutter flask and xts but everything is uppercase now ladies and gentlemen this was basically it hopefully you liked and understood the lesson right and there's a lot of things that you can do with lists right but we would need a whole course talking about every single possibility i just tried to bring what's more common to use with lists see you guys on the next one hello beautiful people welcome back to the course on this lesson we will talk about sets and maps well sets are basically a collection of unique items you probably have seen sets in math so the same concepts applies here in dart or in programming in general so how do you create a set this is how you create a set again in dart you have a lot of ways of accomplishing the same goal so this is just one of the ways you define here set and um this string here as we saw on the previous video basically tells start that this set will be a set of strings here's another example of a set of integers and here is another example of a set of numbers or num values using type interference what's unique about sets again is that it it stores unique elements for example it interprets this three values one one one as one single value which is the number one and you guys will see that in a minute and with sets the order of the elements doesn't matter if i print the prices set the output will be something like this and as i told you as you can see here we have one three times but here we only have one value one so how do we check contents of a set super simple you just write the set name dot contains and the element that you're trying to check and here we are checking if this set contains seven it doesn't so the output here shall be false how do we add and remove elements from a set to remove you just have to set name dot remove and the element that you wish to remove and so add you would do the same thing how do we add multiple elements to a set at the same time super simple instead of having one element we are going to add a list of elements you can see that the output now includes 8 9 and 10. one of the most com one of the most common set operations are intersection and union intersection basically intersection of two sets is another set that contains elements that are present on both sets for that example let's print the set prices and the set prices too so here are the sets as you can see here the only common elements that we have are two and eight so if we intersect these two sets just like this prices dot intersection prices two the output will be another set containing two and eight union is the opposite of intersection instead of just having the common elements union will join both sets and create another set the output here will be an union of the prices and prices two sets now let's talk about now now let's talk about maps maps are collections of key value pairs if that's hard to understand just think of maps as a collection of variables and their corresponding values here is how you create a map again this is just one of the any ways you can do that in case of the map you have to define two types the type of the key which is more or less like the variable name and the type of the data type of the value that will be assigned to that key this is another way of doing it and if you do this this is not is it's not going to default to a set this is going to default to a map and the types of the key and the value will be dynamic because we didn't specify nothing here so how do you initialize a map it's something like this key value pairs if you pay attention here we have a key which is smartphones and the value which is 23 and then we have another key and value pair key value key value so let's imagine we have a map called store stock that keeps track of stock of a particular store and another map that keeps track of corresponding word to digits so this example here comes to show that the key can be anything it doesn't have to be a string we can have integers we can have bull we can have a double data types here has the key if i print these maps this will be the output and this is super similar to a json format so have smartphones which is the key and the value is 23 computers 2 books 32. so how do we access elements of a map here is how it's done the name of the map then we have the square brackets and here inside of the square brackets we insert the key that we want to access the value of so on this example we want to know how many computers we have on stock so we print the number of computers is computer stock and computer stock is equal to stock store stock square brackets computers if i print this the output will be number of computers too and if you come back here the number of computers is in fact two now how do you add elements to a map just like this the name of the map square brackets and then the key that we want the new key and its value if you run the code obviously you'll see nothing because you have to print this the store if we print the store stock and run the code you can see that now we have a new key value pair laptop and the number of laptops is seven how do we remove elements from a map super simple the name of the map dot remove and the key that we want to remove how do we check if a value exists so we have two options we can check if a key exists and that that is done by using the method contains key or we can check if a value exists and that is done by using the method contains value how do we loop through a map now iterating to a map through a map is a bit different for example you may be tempting tempted to do something like this and you would have an error right and the error says that map is used in for loop must implement iterable um i will explain you in future lessons what what implementing iterable means but basically a map is not like a list or a set you cannot just iterate through a map and operate it like this so to have a loop running on a map you would have to do something slightly different so instead of iterating through the map itself you would iterate through the map maps key or the map keys and an example would be for var key in storestock.keys we want to print the key and the corresponding value and again to get the value we need the map name square brackets the key inside and if i run this code this is the output here down here another way of doing that is using for each loop on this case when we are using the for each loop the parameters will be the key and the value and we want to print the key and the value if i run this code this will be the alt and one final example or final way of doing this using four in loops is final entry and we want stock entries right and the entry object contains the key and the value so we can print here entry dot key is corresponding to entry dot value and if we print this we have here the same output so basically here are different ways of doing the same thing and this was the a very quick overview of sets and maps hello beautiful people welcome back to the course on this video we will be talking about higher order functions and in very simple terms a higher order function is a function that takes another function has a parameter or a function that returns another function when we are dealing with collections there are operators that are basically higher order methods because they take another functions as their parameters and one of them is maps and please note that this map is different from map the data type what this operation or what this property of collections does is basically it's going to apply a specific function to every single element of a collection and the difference between a map and a for loop for example is that a map will return another collection of the type iterable while a for loop doesn't have to return another collection let's have an example suppose we have a list of numbers from 1 to 7 and we want to get another list with the triple of these numbers basically three times every single element of the numbers list what we would do is final triple which is going to be a collection is going to be equal to numbers dot map and has the parameter of map we have here this anonymous function element and returns element times tree if we print this here we have the output which is a collection but if you pay attention here the numbers are enclosed inside of parentheses and this is because this is an iterable not a list if you want to convert this into a list you would have to call the to list method and if i print this now we have a list here filtering a collection this is another element of or properties of the collections and basically allows us to perform a filter into a collection and produce another collection as the result of that filter for example let's suppose we want to create another collection with only even numbers from the numbers list how we would do that basically is numbers dot where and the function here is element and returns element is even the difference here is that the where always takes a function that returns a boolean if this boolean is true that element will be added to a new collection and if it's false it's going to be ignored and if we print this again the output is going to be this one here to convert this to a list or set you just have to call the two list or two set method consolidating a collection sometimes we want to join every single value of a collection into a single value for example if you have a collection of numbers we would want to sum all of the numbers of that collection and this is how this is done we have the reduce method let's suppose we have a list of amounts and to consolidate or to sum up every single element here we would use to reduce method and the anonymous function of the reduce method takes two parameters one called sum that it's automatically assigned with the value of the previous function call an element which is the current element that's going to be operated and here we want to return some plus element if i run this code you could see that the output here is 14.95 another method is called fold and the difference between fold and reduce is that if a list is empty and you try to run reduce on that list you are going to get an error that's why is fault is more reliable than reduce let's suppose we have an example called amounts2 and here is fold the syntax of fold is slightly different because the first parameter is an initial initial value that's going to be assigned to sum and then we have the function call so if this list was empty we would have zero here and if we print the output is going to be the same let's try to empty out this list and if i print you can see now that that the result is zero sorting a list basically sorting is organizing a list into a specified order let's suppose have a list of desserts if you want to organize this alpha alphabetically we would call swords on the list so it would be the name of the list dot sort and i say list because sort is not appliable on sets if i print desserts here is the new list and sort can be executed on different types of list list of integers list of anything to be honest and you can have here your custom logic on how you want to sort the elements of the list combining higher order functions or higher order methods and it's possible to use more than one higher order method on a single operation just like this so let's convert the list of desserts into capitals first of all we only want to do this for desserts for dessert names that are higher than five in terms of length and then we want to map those and convert them to uppercase if i print this this will be the output so this was basically it about higher order methods and higher order functions it was a very quick introduction and overview you can go in depth and end of the in any of these topics here now before we wrap up this section let's talk about use cases we have talked about different types of collections but when do you use them simple rule is for lists you want to choose them if the list order is important you're going to use sets if you are only concerned whether something is in the collection or not and sets tend to be faster than lists so if the order of the elements is not important just use sets you are going to use maps if you frequently want to search for a key and get the value because this is stored in key value pairs so that was it see you guys on the next one hello beautiful people welcome back to the course on this video we will talk about inheritance so in simple terms inheritance is the proceed is the procedure in which one class inherits the methods or attributes of another class so the class that it's inherited from it's called the parent class and the inheriting class is called the child class child class let me use this diagram to explain it to you on the first example we have what's called single inheritance where we have a parent class and a child class this class here inherits properties from this class here we have another very common type of inheritance called multi-level inheritance on this case we have a parent class a child class but a child class has a class or a child class of its own this is called the multi-level inheritance and you can have any number of classes inheriting from one another here now coming back to the code this shouldn't be base class yet this is not correct this is child class or subclass anyways um coming back to the code let's imagine we have a class called car with the following properties name and production year then here we have a constructor and a method that it's going to print this is the brand of the car and the production year so our previous approach let's imagine if you want to create another class called suv we would do something like this i mean another class called truck if you if you if we were to create another class called the truck we would do something like this class track a truck and then we would have basically the same structure that we have here on the car class maybe we would add more properties to this track class but it has a lot in common right and if we were to create another class called suv for example we would duplicate this class and add different properties that's not the correct approach with inheritance you can do something like this the class track extends the class car so track is a child of the class car and to define one class as the child of another we use the extends keyword now here we define the default constructor of the track class notice how we are not using this make and this production year anymore because this class the track class doesn't have these properties locally but from the default constructor of the track class we are calling the super class basically what that means is that the default constructor of the track class will call the default constructor of the parent class on this case is the car class and if you watched the video about constructors you have seen that we talked about forwarding or write the redirecting constructors and what we are doing here is super similar to that the only difference is that we are ready writing the parameters of this default constructor here to the super class constructors now this is where the magic happens because here on the track class we can add properties that belong only to the track class for example the bed size of a truck now just to make sure that you guys understand everything i will create another class here called sedan right and same thing the default constructor is going to call the default constructor of the super class and to do that you just do super and then you call the super class like this we pass the parameters here and we will add a property called trunk size right and the difference is that a sedan car doesn't have a bed it has a trunk and a truck it has a bed so these two classes are siblings because they share the same parent the car class again we can create another class called suv that is going to extend cars as well and here we have the same thing the default constructor of the suv class calls the default constructor of the parent class now let's start working with this inside of domain if we create an object of the type car and called print details on that run this code here we have the details this is a ford from 1999 now let's create another object of the type track and give it different parameters here we have it what's nice about extending or about inheritance is that the track class or the track objects can access methods that are defined inside of the car just like this if you go back to the truck class we don't have print details locally on this class but since it's a child of the car and the car class does have this method we can access this method from the trunk the truck class and if i run this code here i have the output we can do the same thing for the sedan now what's nice is that truck has bed size property and we can change that and now we can type something like or we can display we can print something like the bed size of the track name and then the bed size let's see and here we have it now let's talk about overriding methods you already have seen this but let's talk about it again so we are going to take the sedan object and add a truck size trunk size and then we are going to print get details normally just like this boom the expected output is here but if you come here to the class sedan class we use the override keyword we can add a custom body to the print details method for example now instead of printing what we have here class we want to print something else and this is the text that we are going to print basically the the model here the model name and then we are going to print the trunk size if i run this code here i have the output the 2005 suzuki is a sedan with trunk size now what we did here we modified everything inside of the print details class but sometimes you don't want to change the default behavior of a method you just want to add your own logic to it and this is how you do it let's suppose we have a class of an object of the type suv print details if we run this code it's going to print the details as it's expected to do but we can modify this to do something else for example we want to print what's expected this but we want to add something else and then we want to print i love this bad boy and again you can do anything here this is just an example if i run this code you will see that we have the default print details behavior but then we have a custom behavior here that we just added now how to check type of objects during runtime i showed you one method previously but there is another way of doing that you could do something like this suv is car if we run the code the output will be true because in fact suv is a subtype of car and dart considers that a car and in fact an suv is a car suv is truck and obviously this is false because an suv is not a truck if we try to check if suv's suv and suv's object is going to be true because everything in dart it's an object or subclass of object except now now how to check if a variable is not of a specific type and this is how you do it you use the exclamation mark so here we will be checking suv is not a sedan and in fact the output is true because an suv is not a sedan so inheritance is good and all but it has its own drawbacks people usually say prefer composition over inheritance and why is that so the phrase means that when appropriate you should add behaviors to a class rather than share behaviors with an ancestor let's have an example so that you will understand why let's suppose we have a class called person and then we have a subclass of this class called worker now we have another subclass called itprofessional which extends worker again multi-level inheritance now have plumber which extends worker as well it's a sibling of it professional but as you guys can see here if you want to add a new profession or a role we would have to create a different class for that let's suppose a specific person is an i.t professional and a plumber as well you would have to create another class called it professional plumber and let's suppose that person is a youtuber as well then you would create another class called it professional plumber and youtuber right which is bad so what you would do on such situations you would rather do something like this so instead of creating new classes we only create one class called worker let me change the name here and then this class would have a property called roles or positions and here you would add it professional plumber and everything in between that so this is a better approach than doing this and another problem is that inhabitants creates tight coupling and what that means is that when you modify one class you may break other classes for example here if we add a new property to the car class let's say number of doors then the others the other classes just broke in here maybe it's easier to fix but let's imagine you have a very big application with a lot of classes that would be a nightmare so you should pay attention when you're using inheritance and it doesn't mean that you have to use it every single time and everywhere for some cases it's good to use inheritance for example here where we have person and a worker because a worker is a person always so every single property of the person class will be needed for the worker class but sometimes here a plumber and an i.t professional may not be a very suitable example to continue using inheritance so this was basically it an introduction to inheritance on our next video we will talk about abstract classes on this lesson we will talk about abstract classes well up until this point every single type of classes that we used are called concrete classes and basically that means that we can use those classes to create objects abstract classes are different you cannot instantiate abstract classes so think of abstract classes as ideas right ideas are abstract something that you cannot touch but you use ideas to create concrete things right more or less like abstract classes maybe that was not the perfect explanation but in the technical side of things an abstract class must contain at least one or two abstract methods which is basically a method that is defined but it's not implemented does not have an implementation if that's hard just have a look at this example so we have a class here called animal right and to make a class abstract you have to start with the abstract keyword then class and the class name an abstract class can have a concrete property just like this one but it has abstract methods as well and what we mean by abstract methods or methods with the definition but no implementation is something like this if you can see here these methods do not have bodies they don't have carli braces or code implemented into them but can't they can be used if you think about it every single animal eats and moves right but animals move and eat in different ways some swim some fly some walk but they all move right let's continue an abstract class is still a subtype of object so you can override methods inside of an abstract class let's create a class called duck which is an animal so it extends animal but what happens here is that we have an error it says that it's missing concrete implementation of these methods basically what this means is all of these abstract methods must be implemented by the concrete classes that extend the abstract class basically what that means is that if you want to extend an abstract class you must override the abstract methods just like this we override the it method and we are going to print dark it's and the move method usually when you create this you'll have this type of comment thing to do and to do comments are basically to remind you to implement code into certain sections of your app or project on this case we'll print dark moves and what's good about this is that lay eggs is a method that belongs to the dark class but does not belong to the abstract class because not eve not every animal lays eggs but ducks do let's have an example here we have duck we created a variable of the type duck and then we are printing is alive again doc does not have that property here but childs of an abstract class access the abstract values or abstract or i mean concrete classes what i mean is this concrete classes which are child of abstract classes basically a concrete class is a class like doc that you can use to create an object can access concrete members of an abstract class just like this is alive is a concrete member of the animal abstract class if i print this code if i run the code the output will be true but we can also call duck lay eggs which is true as well now let's do something else for example if we create dark two the type here is animal but we are assigning dark to that variable and this works but what happens when you do something like this is that all of the methods that are defined inside of this concrete class cannot be accessible using this variable of the type animal for example if i try to run lay eggs on doc 2 we will have an error but what's fun is that if you print this that will tell you the this is a dark and the tostring method was overreading here inside of the abstract class so during compile time dart kind of doesn't know that this is a doc but when the code is running it will identify dac2 as an object of the type dac that's why when we are calling the to string method here it says that this is the runtime type is a duck and another thing if we do duck 2 dot move even though duck 2 is of the type animal and the move method here is abstract if we run this code the move that's going to be executed is the overridden version of move present on the concrete class dark moves so this is how an abstract class works so people welcome back to the course on this class we'll talk about interfaces in dart well interfaces are basically blueprints of classes what they do is that they define methods and properties that the classes that are going to implement the interface class must have if that sounds a bit hard to understand don't worry you will understand with an example interfaces are extremely useful because they hide the business logic of the code from the implementation what does what that means basically is that they can hide how the architecture works from the actual implementation of the architecture and we are going to have an example here let's imagine we are building a weather app right and we create a class called data repository this is just an abstract class that has a method called fetch temperature and this is a nullable that's why we have this question mark here just as a node interfaces do not have to be abstract classes they can be normal classes as well now what this is going to do is the following we have a class called fake web server and what this class does it implements data repository so when you implement another class you have to override every single method and property of the class you are implementing on that case we will have to do something like this here and we are returning a value right now what happens what's a business logic of an app basically business logic is the core feature of an app on this particular example the core feature of the application would be to return the weather based on the city right so the implementation of the code would be present in here the advantage of using interfaces is that we can mock the data basically we can create fake data on this case we are just returning 42 but if we were to use a real server we would do some http requests here and actually return the weather for the given city right but with interfaces we can just do things like this and mock the data and when we want to take the project to production or when we want to actually connect to a server we would just come in here and change the implementation of the code but nothing else would change outside of this function we have a city the example here is bangalore which is a city in india and then we have here repository is equal to fake web server and the temperature will be repository dot fetch temperature for the city and then we can print the result if we run this code we would have something like this the weather in bangalore is 42 degrees now what's cool is that we already talked about factories right and since data repository is an abstract class we cannot do something like this directly calling here data we cannot do this because we know that we cannot instantiate an abstract class right but since we are dealing with interfaces and we have talked about factories and we know that a factory can return a subclass of a class we can do something like this and everything works from the end user perspective he's just calling the data repository class it doesn't even know that this class is an abstract class but what happens is that this factory here will make it possible so that when someone tries to access the data repository we return the fake web server or we call the fake web server so this is the power of interfaces the end user doesn't know what happens behind the scenes it doesn't even have he doesn't even know that in fact he is dealing with the fake web server because here he's calling the data repository right and from here we are accessing fetch temperature and this method is going to be called on the fake web server and if we run the code the output is going to be the same but the end user doesn't know how the business logic of the application works and what's fine is that for example if we create a real web server another class that implements this we can just come here and change the class that we return here with a real web server and we do not have to change the implementation of the code this is not going to change but the code in here inside of the business logic may be updated and this is the power of interfaces now this may look like a regular abstract class right or you may still be wondering what's the difference of inheritance or using extends and implements and here is the basic difference right let's suppose we have a class right and have a class called another class it has my field has a property and it has a method that prints the field if you extend this class you will have access to every single property present inside inside of the class but if you implement this class you will have to override every single method and property inside of this class what i mean by that is that if i remove this i'm going to have an error and even if i remove just the variable here i'm still going to have an error right so when you implement one class you'll have to override everything that it's defined inside of this class but when you extend that class you you're going to have access you may override some methods if you want to but you don't have to and another difference is that in dart you cannot extend more than one class right you cannot extend let's suppose i create another class here so you cannot do something like this this is not allowed but you can implement multiple classes like this and of course you are going to have an error because you have to implement everything that it's defined inside of the class just like this so this was a very quick overview of interfaces and the difference between extends and implements welcome back to the course on this lesson we will talk about mixins in dart mixins basically provide a way for classes to use properties of a specified class without having to be a subclass of that class and they also allow classes unrelated classes to use common properties and methods to really understand the need of mixins in dart let's follow an example okay let's suppose we have an abstract class called bird and it has two abstract methods fly and lay eggs we have another abstract class called reptile and it has a method called lay eggs now we create a subclass called hummingbird that extends bird then first we will have to override everything here we are overriding the fly method and we are going to print runtime flies basically this will print hummingbird flies and then we are going to override the second method called lay eggs and here we have hummingbird or runtime lace eggs so far so good let's continue now we have penguin that extends bird because penguins are birds as well but then we encounter our first problem when we try to override the method fly because penguins are birds but they do not fly problem number one and we have to override this method we really don't have a way around that and then here we override the lay eggs method again and finally we have another subclass called snake which is a reptile and the snake lays eggs now let's create objects for those classes and access the methods available on those run this code now if you run this code the good news is that the code was successfully executed meaning that in terms of syntax there is nothing wrong but there's a lot of things here that are wrong here we have hummingbird flies which is true hummingbird lays eggs which is true penguins don't fly bro penguins lays eggs and snake lays eggs as well now let's take care of the problems that we have first this is the most obvious one but another problem here is repetition and this goes against the dry principle which is don't repeat yourself if you pay attention here we have lace eggs this method is exactly the same as this method which is exactly the same as and this is a problem right now let's rewrite this using mixings now now the first thing to do here is to clear and clean everything right hummingbird is still a bird and penguins are still birds as well as well as a snake is a reptile so this is fine but on this cases we would do something else to create a mix in we start with the keyword mixing and let's create another mixing for the egg layers so here we have our mixins right super simple now let's have an example of how you would use that we have hummingbirds here and to use mixins we will do we use the keyword with since hummingbirds fly and lay eggs we will have here bird with flyer and egg layer penguins just lay eggs so we will have here bird with egg layer because it doesn't fly reptiles lay eggs but they don't fly some of them at least for this example and what's awesome about this approach is that we avoid cold repetition because egg layers do exactly the same thing and flyers do the same thing as well now let's go back to the example now we can do a very cool thing here let's override the tostring method and now here we have the final example as you can see here inside of the hummingbird class we can directly access fly and lay eggs methods as if they were defined inside of the hummingbird class just like this and for the first example we will do run type flies and lace eggs and here i was just showing you guys that you can call the functions directly inside of the string like this using string interpolation now let's run the code and see what happens here we have hummingbird flies and lays eggs penguin only lays eggs and snake only lays eggs as well right so this is the use of make sense first of all the snake class and the penguin class are not related this is a reptile and this is a bird but they share of a common feature they both lay eggs right on such cases we want to use the same methods on unrelated classes we use make sense and this was an example of mixins in actions in action and hopefully you were able to understand the definition of mixing with this example see you guys on the next one welcome back to the course and welcome to the introduction to asynchronous programming in dart a synchrony in computer science refers to the occurrence of events independent of the main program flow basically what that means is that sometimes you build apps or projects right that will connect to databases will do http requests to servers or will have to read or write very large files right sometimes you may need to wait to see those operations completing for example let's imagine you have a weather app right and how it works is that the user inserts the name of the city and then the app performs a request to a server and the server is going to return the weather of that city right what happens on that case is that after the user enters the name of the city and that's the request you have to wait for the server to respond now having the ability of postponing the the response of the server or just doing something else while you wait for the server to respond that's what's called asynchronous programming in simple terms another term that you are going to hear a lot when you talk about asynchronous programming and programming in general is threads right and now a thread is a sequence of instructions within a program that can be executed independently of other code a section of the code of of your program right a small section or a small group of instructions that are independent from other code in the project are called threads and uh in dart threads run on these sections or chunks of memory called isolates now another concept is is parallelism so here we are going to compare parallelism with concurrency right and parallelism basically uh means that you are is having the ability of running multiple threads at the same time and concurrency runs one thread at a time so here concurrency would execute the thread and then another one and then another one sometimes it may switch between threads but it does one at a time while parallelism can execute one two or more threads at x at the same time up until this point everything that we have been run writing is synchronous code why because we execute the instructions one after the other right with a synchronous code you have the ability of scheduling some instructions or tasks to be executed in the future in dart there's something called event loop and event loop is what dart uses or to keep track of tasks that were previously postponed to be executed in the future to understand this let's have a look at this diagram here this diagram is from the dart a pre apprentice book right and kind of shows the flow of execution of the dart code right here we have the event loop we have something called micro task queue and these are just some internal dart tasks we have the main isolate and this is the code that is being executed and once a task is entered here it can't be stopped and it's executed synchronously one after the other and we have something else called event queue basically these are external events like user keyboard strokes when the user touches on the touch screen responses from databases or servers right so every time we have a new event it's going to be inserted into this queue now how dart works the event loop will be looping obviously and first of all it's going to execute everything that it's inside the main isolate and when we have a new task inside of the microtests the task is going to be transferred from here to the main isolate and the and when the micro tasks queue is empty the event loop is going to execute what's inside the event queue for example if we have new input from the user and stuff like that this is going to be transferred from here to the main isolate to be executed if we have new tasks here while the event the fnq is not empty dart will give priority to the micro tasks queue right so basically this is how it's going to work when we schedule tasks to be executed in the future this is what is going to keep track of that and that will check if we have some things scheduled here to be executed and when it finds something that it's scheduled to be executed it's going to be transferred from here to the main isolate right so this is a very basic overview of how asynchronous programming works in dart right if you didn't completely understand everything again don't worry when we start writing the code i believe that things are going to be much more simpler to understand welcome back to the course on this lesson we will talk about futures on the previous lesson we had an introduction to asynchronous programming and on this one we are going to start working with the practical practical side of things right as i told you asynchronous programming allows us to reschedule or schedule tasks to be executed in the future and futures are what make that possible and in simple terms again future is basically a promise we are telling dart that in the future we are going to return a value or we are going to finish executing something in the future so the syntax of futures is super simple you have your future and inside of this angle brackets you have generic data type you can have any data type here and then you have the identifier and uh this can be used in functions or variables as well the states of a future data type a feature can be uncompleted or promised can be if you're familiar with javascript this is the same thing as promises completed with a value or completed with an error so these are the three possibilities of working with futures the future is either gonna going to be uncompleted or it's going to be completed with a value or it's going to be completed with an error let's have an example i know that this looks a bit complex but basically what's happening here this is an example of adding a delay to your code right and now we have a variable here called my future and then we want to use we want to return a string in the future right for this example we are going to use one of the methods of the future type which is delayed and then we are going to add the duration that we want the value to be delayed to be returned after and here we are just going to define an anonymous function that returns an integer so basically this is how you would work with features and after we execute this code you will see what happens but here we are telling dart that after a second we are going to return a value of the type integer we are going to do that in the future and if you hover over my future you know that you can see that the type of my of the my future variable is a future int now how do you get those values in the future you have two ways of going about doing this and one of them is using callbacks and callbacks are basically methods that take anonymous anonymous functions and do some operations on them having the same example as the one we have up here the only difference is that now the name of the variable is my future 2 right we have couple options first we have the dan method and it looks like this we have one called catch error and we have one called when complete i know that this looks complex but it's super it's super simple then basically this is going to be executed when the result is returned the callback the callback function to be executed is this anonymous function here because all of these properties all of these methods take anonymous functions has their parameters we have catch error and this is going to be executed when the future returns an error and finally we have when complete one complete will be executed regardless of the outcome either even if we have an error or if we have a value this is going to be executed and this should take us back to the three possible states of of a future if we have an error we are going to execute the catch error if we have a value the then it's going to be executed and after the future is complete we are going to execute this line of code here and again inside of this anonymous functions you can do anything that you want but on our case we are going to print value print error if you have an error and print that the future is complete after the future is complete and at the end here we want to print uh after the future so basically this is going to be printed before we execute the future and then we're going to print this let me run the code and i'll show you something interesting now if you pay attention to the output here something kind of weird happened because we had here before the future and then this section here was executed after the future and only then on and only after we printed this the future is executed so what happened if you watch the previous video about the introduction to asynchronous programming we have talked about asynchronous programming being the ability of scheduling tasks to be executed in the future right so what happens here this is executed synchronously this will be inserted into the event loop and this will be executed and after the two seconds have passed this will be removed from the event loop into the main isolate and this is how it works so and this is the power of asynchronous programming while we wait for this to return something we could be doing something else and this is what happened here while we waited for this output here we were executing this here let me run the code again so that you'll see you see how we had this almost at the same time instead instantaneously we had these two outputs here and then only after two seconds we had this here right so instead of waiting everything and uh waiting the execution of the code to get this the output of the future function we just skipped this we executed this section of the code and when the value was returned here that's when we printed the value here and we said that the future is complete now another way of getting values from futures is to work with the await and async keywords now how that is done is like this first you can see that the main function here is different because it returns a future void and then we have a sync basically we use async when we are going to work with feature future values inside of of the function right so if the function is going to deal with future values it has to return a type future it can be void but it has to return a future and we have after the function name and after the parenthesis we have to insert this keyword here async right basically means that this function here is asynchronous going back to the same example we have here before the future and then we have the same method that's going to return 42 after a second then we are going to print the value and then after the future now there's something else happening here if i hover over the value you can see that the data type here is in but if i hover over this previous when i have it over this previous field here you saw that the data type was future int and what makes that difference is this keyword here called await because this function is going to return an integer we are awaiting for the value that's going to be returned from here and the value is int that's why this data type here is int and it's not future int if i remove this keyword and i hover over this you can see that the data type now changed from int to future int so when you want to get the actual data type value that's going to be returned you use the keyword await if i run this code you'll see that something happens now unlike the first time where we printed before the future we skipped this code to execute this and we came back here when you use the await keyword here we are actually going to wait to see this code here being executed and when it's complete the other one is going to be executed let me just increase the duration here and you'll see and as you can see we had to wait for three seconds and then this was executed now when you follow this approach when you use a weight and a async how do you handle errors just like how we had this thing here dan catch error and when complete now i'm going to introduce a new concept called error handling right and to do that we are going to use try and catch blocks they are going to look more or less like this this is the syntax we have try and inside of this block here we are going to try to do something or to execute a task catch if we do have an error the code inside of the catch block is going to be executed and finally it's going to be executed either with an error or without so either way this section of the code is going to be executed so this is equivalent to the one complete method here right this is the syntax let's have an example before the future after the future and here we have the try and what we want to try it's going to be inserted in here if we have an error we want to execute the uh this code here and finally we want to execute this code here now what we are going to try is the exact same function to return a value after a delay and then we want to print this value if we have an error we want to print the error and we want to print future is complete after everything is executed if i run this code as you can see here we have before the future value is 55 future is complete and then after the future right now there's something called exceptions and sometimes this is called exception error handling or exception handling right i'll give you an example let's suppose you're building a calculator app you know that we cannot divide a number by zero right so you would have that operation here and you know that sometimes users tend to divide numbers by zero instead of crashing the app because if you don't handle that the app will just crash you could create an exception so basically you would tell the computer that if the user tries to divide a number by zero instead of crashing the app execute the code that it's inside the catch statement right on our case how you would throw an exception on that case this is how you throw an exception so let's suppose there is a condition that is going to cause an error instead of stopping the execution you would write here throw exception and then here the exception name and what's going to happen is after this line of code here nothing after this is going to be executed inside of the try the execution will go directly inside of the catch block let me run the code and you'll see that in a minute and that's why i have the message here saying that code because this is not going to be executed and you can see that i have here before the future and then we have there was an error and that's what is printed here that that's the exception and then what's inside of the finally block is executed and we have here after the future so this is a brief introduction into error handling and exception handling right but when you use a weight and a sink this is how you would do with every single you would try to deal with every single possible output of your futures and error handling or exception handling is not restricted just for futures you can do this with normal operations as well so hopefully you guys understood the basic concepts and introductions of this and we will continue talking about this on next lessons you
Info
Channel: Santos Enoque
Views: 14,860
Rating: 4.968051 out of 5
Keywords: dart tutorial, dart language tutorial, dart language, dart language for beginners, dart language for flutter, dart language full course, dart language tutorial for beginners, dart language basics, dart language future, dart language course, dart programming language, dart programming, dart programming language tutorial, dart project, dart programming language tutorial for beginners
Id: igauQ_rF_bU
Channel Id: undefined
Length: 235min 24sec (14124 seconds)
Published: Wed Jul 14 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.