Learn The Dart Programming Language - Complete Free Course!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to the course my name is john elder i'm the founder of codyme.com and in this free and complete course i'll walk you step by step through the dark programming language we'll start at the absolute beginner level and then quickly move through intermediate right up to but not quite including advanced topics why dart because dart's the programming language used in flutter and flutter is the amazing framework that lets you build cross-platform gui apps quickly and easily so you need a solid understanding of dart in order to really use flutter and that solid understanding of dart is what you'll learn here today this course is made up of 14 sections including dart installation variables data types lists maps loops logic functions user input string to integer conversion user input type conversion fizzbuzz classes and object-oriented programming and classes without initialization after this course you'll understand the dart programming language well enough to dive headfirst into flutter so you can start building awesome gui apps if you enjoy this course check out my website kodamy.com where i have 50 other courses on web development gui app creation database development and more use coupon code youtube50 to get 50 off memberships on my courses videos and books for one time fee which is insanely cheap so let's learn dart [Music] so in this video we're going to install dart we're going to install all the packages and things that we need to start to use dart get it up and running and then move on from there so i'm in my git bash terminal and i'm going to create a couple of directories first i'm going to mkdir make directory i want to put this in the c drive and i'm just going to call this dart this is where we're going to download and install dart the actual programming language and all the stuff that we're going to need okay while i'm here i'm gonna make another directory put it in the c drive and let's just call this dart stuff this is where we're gonna save our dart files our programs as we create them as we learn dart all right that's good now let's head over to our web browser and i'm just gonna go to google and type in dart install or something like that and then here's the website we need and this is dart dot dev slash get dash dart if this changes by the time you watch this video just google it and you'll find it and what we need is the dart sdk now i'm on a windows computer so we'll be doing this for windows there's a bunch of different ways you can do this but you can see there's some command prompts you can use if you have chocolaty installed we don't uh you can see linux and mac commands that you can do it from the terminal we don't have those either what i'm going to do is i'm going to come up here and i'm going to grab any release channel by downloading the sdk as a zip file so i'll click that now i'm at the get dash dart slash archive page and you come through here and it's a little confusing right there's a bunch of different options we just want the stable channel there's other channels you can get the dev channel the beta channel those aren't as stable we want the most stable current version and that's just the stable channel right here so we come through here and i'm on a 64-bit machine so i want this dart sdk so you can see you can hover on both of these i just want this one here the first one dart sdk so i'm going to click this and we're going to save this you could save this anywhere but we've got that directory we created so i'm going to navigate to that that was c dart there it is i'm just going to save it there and this is a pretty decent sized file it's a 186 megabytes it looks like and so this is firefox and so my downloads are up here if you're on chrome they're probably down there in the corner wherever they are in your browser click this thing to open it and here we go now this is a zip file so it's all zipped up we need to unzip it we need to extract it so i'm going to click extract all and it's going to ask me where i want to put this now this is fine i'll just extract it here now this is like 400 and something megabytes it's a big package to install so make sure you have a lot of space in your computer eventually we're going to learn flutter and you're going to need i don't know like 10 gigabytes for that or something so you're going to need a lot of space on your computer so this should just take a second okay there we go and here i'm going to navigate back to dart and you can see here's the zip file i'm just going to go ahead and delete that and if we click this and then we see this dart dash sdk and what i'm going to do is i'm going to cut this so right click cut and i'm just going to come back and i'm going to paste this in the main dart directory you don't really have to do that but yeah i like to keep these things nice and clean so you can see now this one here is empty so i'm just going to go ahead and delete it okay so i'm in my c dart and we have this dart dash sdk now we need to add this to our environmental paths on windows so that our terminal will know where to find it when we want to run dart programs right so the way we do that is we click on the double click this and we want to go to bin right and so here we can come up here and right click and then what we want to do is copy the address as text we just need to copy the address on our computer where this is sitting and we know it's sitting at c slash dart slash dart sdk bin but instead of typing all that out we can just right click on here and click copy address as text next you want to go and hit your windows start menu this is actually on the other monitor so you can't see it but the windows start menu pops up you can imagine that just type in env short for environmental and a little icon will pop up that says edit the system environmental variables and that's on your control panel so just click that icon and this thing pops up and this is where our environmental variables are we can see right here so we click on that and then look through this list and hopefully you'll have a path if not you'll need to click new and add path but you should already have path on here and what we need to do is add dart to our path so i'm going to click this and click edit and when i do this box pops up with all the other variables environmental variables that we have i'm just going to navigate over here and click new you can see this little thing pops up here i'm going to right click and click paste and it pastes in that address we just copied from the windows explorer which is c dart dark dash sdk bin so go ahead and click ok and now you're good to go now if we click edit again you can see hey it's still there make sure it is it is okay so we're good so we can click ok here get out of this and we can close this we're done with all of this so we're now installed now i like to use sublime text a lot of you guys like to use things like visual code and stuff like that you could probably get away with that for dart but i'm just going to use sublime so we're going to need to install the dart package for sublime just so the code looks nice and i'll show you why in just a second but first if we head over to our terminal what i'm going to do is i'm going to close this terminal because you probably need to restart this since we've added an environmental variable to the path so i'm going to go ahead and close that i'm going to open another one and i'm just going to type in dart and hit enter and if something pops up and it says a command line utility for dart development we know hey dart has been installed correctly if you get some sort of error dart probably hasn't been installed correctly you didn't unzip it correctly or you didn't add the right variable path to our environmental variables or something so just rewatch what we just did and give it another try but you should probably have this that looks great so i'm going to now navigate into our c dart stuff directory because that's where we're going to start creating things and let's head over to sublime text and i just opened a new file and i'm going to go file save as i'm going to navigate to our dart stuff directory and i'm just going to call this hello.dart now dart files end in dot dart like python files into dot pi go files and then dot go ruby files and then dot rb dart files into dot dart so let's just create a very simple dart program just to see if this is working because it's probably not yet so let's go void main and we'll get into what all this is in a bit but here i'm just going to go print and then we'll do our hello world file because that's always the first file and dart programming we have to put the semicolon at the end that's going to drive you crazy now if we save this you'll notice this is all white and that's not what we want we want this to be different colored based on the code like all other files do when we write code and the problem is sublime doesn't know what dart is so we need to install the dart package in order for it to know what this is if we go to view and syntax we could see you know there's no dart listed here so we can do that let's head over to preferences and hit package control and then just type in install and you can see this install package thing pops up so i click that and this little box pops up now we could just type in dart and there's a couple of options dart and dart light i don't think it probably matters all that much so i'm just going to go ahead and click the regular dart one and click that this thing pops up there's some more instructions here we don't really need to do this because we're not going to be running a terminal in sublime because that's always a terrible idea so we could just close this and now if we come back over here and click view syntax we can see hey there's dart and when we hit it boom it changes color as you would expect all coding things to change color so you may have to restart sublime in order to do this i didn't have to this time it looks like so we should be good to go so i'm going to go ahead and save this file head back over to our terminal and let's ls to make sure yep there it is there's that file hello dart so to run dart files we just type in dart and then the name of the file hello.dart and boom it prints out hello world just like we would expect it to because hey even though we don't know dart yet we know that hey that probably is going to print out hello world to the screen and sure enough it did and we're good to go so that's how to install dart on a windows machine very easy especially if you're using sublime you know if you're using visual studio code you're gonna have to install some packages and do the whole thing and it's sublime is so easy that's what we're going to be using just to learn the dart programming language later on when we start looking at flutter we might use something else like visual studio code or something but for now we can get away with sublime and so that's what we're going to do so that's all there is to it i think dart is going to be a lot of fun for you guys to learn and again dart is the programming language that flutter uses and if you're familiar with flutter flutter is that great framework that you can use to make mobile apps very easily they work on android they work on ios they work on everything right out of the box that's the great thing about flutter but first we need to learn the dart programming language so we're going to spend a little bit of time learning dart it's quick and easy it shouldn't take very long at all and it's a lot of fun in this video we're going to look at variables in dart and variables in dart are very easy but there's a few things you need to know and that's we're going to look at in this video so dart is more like a c type programming language so a lot of the c type conventions hold true and dark just like in go we've been looking at recently so you're going to usually use camelcase to name your variables there's a couple of things they can't start with numbers they can't use special characters except for the underscore and the dollar sign and that's pretty much it you can't use keywords right same as all programming languages so to use a variable in dart you have to first declare it and there are many different ways you can declare variables in dart so let's just kind of start out with strings here so one way to do it is to use the var keyword and that's short for variable right so var and then just name your variable and then set it equal to whatever you can also be very specific instead of using the var keyword you could declare what kind of data type this is going to be so if we know this is going to be a string we could also go string name john that works as well if we know it's going to be an integer same thing holds true we could var x and set that equal to 41 or we could be explicit and say int x equals 41. all right so if we wanted to comment this out and give this a try we could just come down here and print x remember all of our statements have to ending semicolons in dart and let's also comment this guy out if we head over to our terminal i'm in my c dart stuff directory if we're on dart vars.dart we get 41. likewise we can uncomment this guy out and comment this guy out run this guy again we're gonna get the same thing just 41. right there's also something called a dynamic right so if you're not sure what the data type is and you don't want to use the var keyword you can use the dynamic keyword and same deal you know we could say i don't know first name set that equal to tim whatever so generally i just stick with var but dynamic is a thing and well there you go finally we have something called constants and finals now constants are keywords that you're never going to change right you set them once and they just they can't be changed they just are what they are and final is the same thing it's just slightly different so if we go const and final we could set one up for const like this with constant keyword and then you know same deal same things we could use string or or not uh and we could say i don't know full name [Music] equals john elder likewise we could use the final keyword and again we could call a string and uh i don't know nickname equals j right whatever so what's the difference between a constant a final well it's a little complicated consts are if you know what it's going to be at compile time use a const if you know what it's going to be at run time use a final so think of like i don't know a date or something when the program runs the date is going to be different every time you run it well in that case use a final because you're going to the date's going to change if the date is never going to change and you just want the date that's going to be when you compile the program use the cons that's a sort of not a complete answer but it's good enough for now the difference between constant final and that's all there is to it so variables again very simple you're usually just going to use the var keyword again you don't have to use the data type in here dart will imply it but you know it's always best practices to use the data type if you know it if you're not sure what it's going to be you can use a dynamic finally one last thing declaring and not using so we can go var my name and then just not assign anything to it and down here if we wanted to print out my name but we can come down here and run this guy again and we'll see it's going to return null so you know the variable doesn't have anything assigned to it it's just null it's not anything right so that's kind of handy for a lot of different things you can assign something to it later on if you wanted to you know we could come down here afterwards and say you know my name equals john come up here copy this guy print it in a second time the first time it'll be null the second time it'll be john because we changed it and we see that here null and john so those are variables in dart pretty simple pretty straightforward and that's all there is to it in this video we're going to look at data types in dart and data types are pretty simple in dart there's not a whole lot of them and they're pretty straightforward and we're going to go through them in this video so basically we're looking at numbers strings booleans list maps and dynamics now numbers we have integers and doubles and we'll talk about those in a second strings pretty straightforward text you know booleans is always true or false and then we have lists and maps and lists are arrays you know in python they're called lists and every other programming language they're called arrays they're called lists in dart and maps are basically dictionaries in python or hashes and ruby they're key value pairs so we'll probably talk about lists and maps in another video because they're a little bit more complicated finally we have dynamic if we haven't explicitly stated what the data type is dart will imply that it's dynamic meaning i don't know sort of like a catch-all right so let's go through here there's not a whole lot to know about these things but there's a few things so we'll go ahead and look at them one at a time in this video so i'm using the sublime text editor and then get bash terminal as always and as always you can find a link to the code in the pin comment section below as well as a list of the playlists with all the other dart videos in this series so check that out if you haven't so far so we've got our basic starter code that we've always got here i'm just calling this file dt.dart short for data types i guess and so let's just start out with a string so we start out by calling the string keyword and then we give this variable a name i'm going to call it first name and we set it equal to you know whatever we've already looked at this before so pretty simple there so i can just print and then let's go string just to keep track of these and then we can call first name inside of this quotation marks using the dollar sign symbol like that so if we save this head over to our terminal i'm in my c dart stuff directory and we can run dart dt dot dart and it pastes out string john right pretty simple pretty straightforward so now let's talk about numbers numbers you have two options you have integers which are whole numbers 5 27 108 and you have doubles which are decimals 1995 right so to use integers we use the int keyword and i'm just going to call this mynum we can set it equal to anything we want so i'm just going to grab this guy paste it in again here let's go integer and change this to my num so you know obviously if we save this and run it we know what we're going to get integer 41 right pretty straightforward doubles are like i said decimal point we use the double keyword and then i don't know other num and we can set this equal to 1995 or something right a decimal number so again we can print double and then go other num let's change this to numb there we go save this run it pretty simple double 1995 right that's your double next we have booleans and to use a boolean we call the bool keyword right bool short for booleans and then i don't know let's just call this mybull and we can set this equal to anything we want so notice this is lowercase we can also do true also lowercase i'm going to switch it back to false because you know false is more fun and we can print out boolean and then my bool and if we save this and run it of course it's going to print out false right pretty simple finally we have dynamic and like i said dynamic is a little weird it's if you haven't decided what the thing is then you can use dynamic you know there are situations where you might want to use this i don't find it all that useful but you know there are times when you might need to use it you just use the dynamic keyword and then i don't know let's call this full name something like that let it equal to john elder and again we can print this guy to the screen dynamic and then full name so of course if we run this you know we're gonna get john elder so those are your main data types that you're gonna be working with like i said we're gonna talk about lists and maps in their own separate videos because they're a lot more complicated than these basic data types and you know it's very straightforward you have strings you have integers you have doubles booleans and dynamic like i said you're probably not going to use dynamic all that often but you'll definitely use booleans you'll definitely use doubles inch and strings all day every day if you're using dart and it's just that simple to use them in this video we're going to look at lists in dart and there are a couple of different types of lists in dart there's fixed length and there's growable we're not going to talk about fixed length in this video or probably any video because i think that's stupid we're going to look at growable lists meaning you can add things and remove things and grow them and shrink them as you want and of course a list in dart is just an array that's what dart calls them sort of like python and they're just a list of items so let's head over to our code i'm using the sublime text editor and then get batch terminal as always and as always you can find a link to the code in the pin comment section below as well as link to the playlist with all the other videos in this dart series so check that out if you haven't so far so i've got a file called list.dart it's just our basic dart starter code that we've always got and i've got a few things here that we're going to kind of fill out as we go along so like i said we're just going to look at growable lists lists that can be changed and added to and things like that in this video so let's create a list we call var and then just name it i'm going to call this my list and then we just set this equal to whatever we want so we can say one two three whatever right now we can print my list and this will just print out the actual list if we want to print out a specific item from our list we could you know my list and then use our notation like this if we want to print out the first item that's zero lists and arrays always start at zero so this is the zeroth item one if item and then two with item all right so zero one two and that's all there is to it let's go ahead and save this head over to our terminal i'm in my c dart stuff directory and let's run dart list dot dart and we can see it just prints out the actual list and then the zeroth item which is one right okay pretty simple pretty straightforward as you would expect so let's say we want to change one of these items we want to change this one to 41. well super easy we just call my list pass in the item we want to change we want to change the zeroth item we can change that to 41. now we can come up here and print our list again to make sure that that actually happened run this guy and we could see it used to be one now it's 41. pretty simple we can also create an empty list so let's go var empty list and set that equal to nothing we can print this guy out if we want when we do head back over here around the sky again we can see we've got an empty list so all right that's cool one of the nice things about growable lists we can start with an empty one and then we can add stuff to it later on right so if we want to add something to our empty list we could just call empty list and we use the dot add function and then let's just add something the number 41 right so if we want to print this guy again come back over here we can see it had nothing in it now it has 41 in it pretty cool that's how to add one thing to a list what if we want to add multiple things right you think well can i just do something like this no you can't not quite what we're going to want to do here is use the add all function so we call our empty list dot add all notice the second a here in all is capitalized and then inside of here we're gonna pass in a bracketed list so we can go one two three so this will start out with 41 and then add one two three and we need our semicolon there and if we want to save this and run it we can head back over here and let's run this guy we see it started out with zero we added one item it's 41 then we added a bunch of items 41 1 2 3. and you'll notice this one two three we put them in brackets when we use the add all but when they're in the actual array they're not sort of separated in their own bracket they're just part of the main list the main array so okay that's cool so we added things here but we can also add things at specific positions inside of our list remember lists or arrays are indexed items so they're index numbers so we can choose which index number to place an item if we want to place an item and we do that using insert let's come back up here to our main list which was one two three and then we change the first one to 41. let's come down here and let's go my list dot insert and here we pass in the position and the item so remember our my list there's zero one two if you wanna add something to the end that would be at position three and we can add something like you know the number nine hundred something like that here we can now print my list see if that worked back over here run this guy again and we should sure enough at the end we added 900 here's our original list and we changed it to this and then we changed it finally to this right so if you you know want to add something in a specific spot you can use that you can also insert many things with insert all sort of like we had add and add all we also have insert and insert all so we could go my list dot insert all and the same thing you add in the position you want to put it in so if we want to put it at the one position which is the second position remember and then inside of here again we just create a sort of list and we can go 99 98 97 i don't know whatever and then let's just print my list head back over here let's clear the screen around this guy and you can see here in the one with position we've added 99 98 97 so we've got forty one and nine ninety eight ninety seven two three and uh and then nine hundred so that's cool and also before we go on here let's you can also have mixed lists up until now we've just been using integers just because it's easy to do but you can create a list with mixed data types so you could have integers you can have strings you got booleans you got anything you want right so let's create a mixed list very quickly let's just call it mix to list and we want to set that equal to one two three and then i don't know john and bob right so here we can print out mixed list save this guy head back over here run it again and we see one two three john and bob so that's cool so moving on we can also remove items from a list so let's take out john right so that's very easy we just go mixed list dot remove and then just pass in whatever we want to remove so we can take out john now this works fine if there's only one john in here right so keep that in mind so if we want to now print this guy out again let me just kind of copy this save this head back over here run this guy we see here we have john in here john has been removed well okay that's great like i said if there's only one john but if there's more than one john you need the specific location to remove right so we can do that using remove at so let's go mixed list dot remove at and here we just pass in the index number that we want to remove so we've got the let's say we want to take out this two that's the zero one if item we just pass in the one item like that we can print this guy out again onto the screen and see if two has been removed sure enough one three and bob we had a two we took it out and now it's gone so those are lists in dart pretty easy pretty simple pretty straightforward very easy to use and that's all there is to it in this video we're going to look at maps and darts and maps are key value pairs so if you're coming from python a map is a dictionary right same exact process and they're very easy to create and use in dart and that's we're going to do in this video so so let's head over to our code i'm using the sublime text editor and the git bash terminal as always and as always you can find a link to the code in the pin comment section below as well as a link to the dart playlist with all the other dart videos in this series so check that out if you haven't so far so i've got our basic dart starter code i'm calling this maps.dart and these are the things we're going to look at in this video how to create a map how to show the values the keys get the length how to add something how to add many things how to remove something and how to remove everything so to create a map in dart we just use the var keyword and then just name it i'm going to call this toppings so we're going to create a map with pizza toppings so everybody's favorite pizza topping right so we start out with john john is the key remember these are key value pairs they're two things that are paired together so we separate them by colon so john's favorite pizza is let's say pepperoni so that's one item in our map right the next one let's say mary and let's say mary likes cheese pizza that's it that's all there is to it so now we can print our toppings if we like that will print the entire map or we can pull out a specific item from our map so we can print toppings and then we use the square brackets and then just pass in the key that you want to print out so if we want to see what john's favorite pizza is we just pass in john so let's go ahead and save this head over to our terminal in my c dart stuff directory and let's just run dart maps dot dart and you can see first time we print out the entire map we get the squiggly brackets and all here we just print out john's favorite pizza see john likes pepperoni sure enough john likes pepperoni so super easy to do and that's kind of all there is to it now we can also see the values the first things are the keys the second things are the values so pepperoni and cheese those are our values if we want to print out just those values we can go toppings.values pretty simple go ahead and save this head back over here and run it you can see it prints out pepperoni and cheese we can do the same thing with our keys we can print out toppings dot keys and this is going to print out you know john and mary if we go ahead and save this run this guy again sure enough john and mary if we're curious how many items how many things are in our map we could always just print out toppings dot length which is very useful save this round again we see there's two items two key value pairs one two all right that makes sense pretty simple so let's say we want to add something so we've got our toppings and let's say we want to add tim right so what does tim like i don't know if tim likes sausage so then if we print out toppings make sure that was added head back over here clear the screen run this again we can see originally here's our map then we added tim and now here's our map him has been added pretty simple we can add many things and we use this and to do this we use the toppings dot add all function and inside of here we pass curly brackets and then basically we just create another map in this function and it will add it to our old map so if we have for instance tina maybe tina likes bacon on her pizza and let's say maybe we have steve and steve likes supreme pizza i don't know whatever now we can print our toppings see if all of those things got added head back over here run this again and we can see oh man it's getting big so we did have john mary and tim and we added tina and steve so that's cool if we want to remove something we can do that using the remove function so let's just go toppings dot remove and let's say we want to get rid of steve we just pass in steve with quotation marks and we can print our toppings just to make sure steve was removed back over here run this guy again so here we see steve and we removed him printed out again no more steve so that's pretty cool finally we can remove everything so let's just go toppings dot clear and that's just a function and now if we want to print our toppings we can see hopefully this will remove everything back over here run this guy again we see we just get an empty little brackety thing there because everything's been removed so those are maps and dark key value pairs very easy to use super useful you're going to use those for absolutely everything they're a cornerstone of all programming all programming languages have maps or dictionaries or hashes sometimes they're called key value pairs whatever you call them they'll act the same way and that's how you do it in dart in this video we're going to look at loops in dart and we're going to look at for loops the 4 in loop and the while loop and loops are of course a fundamental thing of all programming languages they allow you to loop through things and do things over and over again for a certain amount of time or a certain number of times or whatever you want to do in your loop so let's head over to our code i'm using the sublime text editor in the git bash terminal as always and as always you can find a link to the code in the pin comment section below as well as a link to the playlist with all the other dart videos in this series so check that out if you haven't so far all right so i've got some basic dart starter code that we always have and i'm just calling this loops.dart so let's start out with a for loop and we're going to start out by creating a variable i'm just going to call it num and then let's set it equal to like 5. we want our loop to loop five times right so we create a variable for that now you don't have to create a variable you can use a number but you know it's kind of standard to use a variable so that's what we're gonna do so let's go four so inside of here we need to create sort of like a counter variable to keep track of how many times we've looped so i'm going to call this i that's sort of you know normal and let's set i equal to our num right now instead of setting it equal to num because we've got the variable num you could just you know set it equal to five but you know it's kind of standard to use a variable here next we need to say what are we actually kind of looking for in our loop so let's go as long as i is greater than or equal to 1 we want to keep looping right now every time we want to go i minus minus this will subtract 1 from i so i starts out at five we loop through i becomes four we loop through again i becomes three we loop through again i becomes two we loop through again i becomes one i is still greater than or equal to one so we loop through again i becomes zero at which point i is no longer greater than or equal to one and the whole thing stops right basic loop structure so next we use our brackets and inside of here we do whatever we want to do while we're looping so let's just print out i right all right that's it that's all there is to it very simple go ahead and save this head over to our terminal in my c dart stuff directory and let's run dart loops dot dart and when we do it prints out 54321 right exactly what we would expect to happen so very cool very easy and that's all there is to it now a lot of times when you're using a for loop you're going to want to do it to iterate over something so you've got an array or a variable or some list of something you want to iterate through certain amount of times well for that you're going to use the for in loop so let's create a quick array and i'm just going to call it names and let's just add two or three names here so we've got john we've got tina and we've got say steve right now we want to iterate through this list and print out each item in the list in the array right well we can use the four in loop so let's go four now we need to keep track of each item that we iterate through so we're going to create a variable i'll just call it name since this is an array of names i'm gonna use the singular name we could just as easily call it x right but be descriptive let's say for name in names so this is our names array this is the for in loop see the n right there and that's all there is to it so what do we want to do well let's just print each name right so let's go ahead and save this and over here run this guy and we see john tina and steve pretty cool pretty easy and personally i'll use a four in loop more often than i'll use a regular for loop because when you want to loop oftentimes you want to loop over something you know here we've got an array we want to loop through the array and print out everything do something with each item in the array you'd use your for enloop for that so all right that's the four in loop finally we have the while loop and this one's really easy we just call while and let's say while our num is greater than or equal to one remember our num is up here it's still five we can use that again and then we just do whatever we want to do so we can print for instance num now you'll get an infinite loop if you do this because we're not changing our counter right so we need to change our counter let's go num minus minus so every time we loop we want to subtract one so then eventually we'll get the same things up here it'll start out at five four three two one then it becomes zero zero is not greater than or equal to one and the whole thing stops right so okay pretty simple go ahead and save this head back over here run this guy one more time and we see sure enough five four three two one and then it stops and that's all there is to it so those are the main loops in dart there are some other loops as well there's a uh do while loop that's basically a while loop that's a little bit different i never use that i don't know anybody that's ever used that i've never seen that in code so i'm not going to talk about it but if you're interested you can google the do while loop and dart most programming languages have a do while loop i never teach it in any programming language because i don't know these three will get you through like 99.9 percent of anything you would ever want to do so that's what we do in this video we're going to look at logic and dart before we get started if you like this video you want to see more like i'm sure do smash a like button below subscribe to the channel give me a thumbs up for the youtube algorithm and check out codamy.com we have dozens of courses with thousands of videos that teach you to code use coupon code youtube50 to get 50 off memberships all my courses videos and books for one time fee which is insanely cheap okay like i said in this video we're gonna look at logic and dart if statements if else statements if else if statements right so super easy with dart this is just a you know a core fundamental computer programming concept all programming languages have logic if statements things like that and in darth they're pretty straightforward and they're pretty easy to use so let's head over to our code i'm using the sublime text editor in the git bash terminal as always and as always you'll find a link to the code in the pinned comment section below as well as link to the playlist with all the other dart videos in this series so check that out if you haven't so far so i've got our basic dart starter code and i'm just calling it logic.dart so let's start out by creating a variable i'm just going to call it num and let's just set it equal to 5. so let's start out with a basic if statement which will allow us to basically check for some condition ask some sort of question and then take some action based on the answer right basic logic so let's go if and we use our brackets and now we set our condition so let's say if num equals and we use the double equal to sign it's pretty common in all programming languages if it equals five then we want to do something and we use our brackets to show what do we want to do well let's just print to the screen i don't know the number is five all right so whatever we can go ahead and save this head over to our terminal in my c dart stuff directory and let's run dart logic.dart and we say hey the number is 5 which you know makes sense because we know our number is 5. we change this to 3 save this run it again nothing at all is going to happen you can see just nothing happens at all because our number didn't equal 5 right our number is 3. so if this is true then this will get executed if this is false then it will skip down to right here the outside of this bracket here and then just continue on with the program we don't have anything else in our program so it just sort of ends right so that's a basic if statement let's go ahead and change this back to five we can also use an if else statement and this allows us to take some other action if the first statement isn't true if this is false do something else right an else statement so to do that we just call else we can then do something else the number is not five all right if we save this what do you think is going to happen the exact same thing as before it's just gonna say the number is five but if we come back here and change our number to something else say four or whatever save this come back over here run it again now it says the number is not five and we can get really fancy with this if we want we can say the number is and then you know and we can i don't know stick our num in there or something like that and back over here run this again because hey the number is four right so very fancy very sophisticated all right and that's all there is to it so i'm gonna change this back to the number is not five there we go so that's if that's if else statement we can also do an if else if which allows us to ask more than one question right so here we're just asking one question is num equal to five right we can do other things as well so i'm going to take this down here and then after this we just call else if and then we could do another condition right so to do that we just again use our brackets we could say hey this time test to see if num equals three i don't know right if it does then print out the number is three right and then we can bring this back up here so we still have our else statement and that's all there is to it so if we look at this we could say change this to three we say hey is the number five if it is print out the number is five well it's not so it's going to skip this and it's going to come down here to this line let's put this up here and then it's going to say oh well we've got another condition here is the num equal to 3 if it is print this out and then stop if it's not follow along down start with this line right here and then the number is not 5. so if we go ahead and save this we can pretty much guess what's going to happen here we can see the number is three and we can play around with this you know we could change this back to five if we do that you know it's going to say hey the number's 5 of course and we could change this to 51. all right go back over here save this hey the number is not 5 or if we really wanted to we could get crazy and say the number is not three or five it is and then we could put in our num all right go ahead and save this run it clear the screen the number is not three or five it is 51. a silly example but i think you get the picture very easy to use logic with dart as it is in most programming languages now we're using the equal to here we can use all of our comparison operators right so you can use greater than less than greater than or equal to less than or equal to not equal and all of that so you know you could say hey is number greater than five well in this case it is it's 51 and you get the picture or greater than or equal to 5 right whatever that's how that works so that's logic with dart pretty simple pretty straightforward and pretty fun in this video we're going to look at functions in dart and functions are a super important part of any programming especially if you do functional programming versus class-based programming and they're pretty simple to do in dart so let's head over to code i'm using the sublime text editor and they get batch terminal as always and as always you can find a link to the code in the pin comment section below as well as a link to the playlist with all the other dart videos in this series so check that out if you haven't so far so got our basic dart starter code i'm calling it funk.dart and now we just want to create a function so i'm going to call it myfunk right and then we need parentheses and then brackets and inside of the brackets we could do whatever we want to do in this function so let's just say print hello functions very interesting right exclamation part so that's a function now if we save this head over to our terminal and run dart func dot dart nothing happens and that's because with functions they only get run if you specifically call them and we didn't specifically call this function we just sort of defined it here so to call a function you just name it so my func and you use your parentheses and that's it so now this will call the function it should print out hello functions onto the screen and that's that so let's come back over here run this guy again and sure enough it says hello functions so here we're printing hello functions we could just as easily return hello functions now if we run this nothing's going to happen run it again nothing happened why well because in this instance here we're returning this we're not doing anything with it so if we wanted to actually print that we would have to wrap this entire thing in a print function then it would print out what it has returned here which is that text so now if we head back over here it's going to say hello functions likewise we could make this into a variable so we could say var i don't know thing and set it equal to my func right again same thing if we ran this again nothing's going to happen because we've just assigned it to this variable we didn't actually do anything with it so if we wanted to do something we could for instance print out thing if we did that and ran it then now it's going to print out hello functions again so just your basic function stuff now there's all kinds of different things we can do with functions here we're just sort of this is a very boring function it just says you know hello functions right so instead we could create some variables and pass those things into it so let's say we want to create a string and we're going to call it name now inside of here instead of saying hello functions we could say hello name now down here when we call our function we have to pass in a name right and since this is a string this has to be a string so now this will pass in the string john into our function it will assign it to this variable name and then inside of this function we can use that variable in any way that we want so now if we save this and run it it's going to say hello john very cool right basic functionality for a function all right that was a terrible joke so you know we could pass more than one thing we also want to pass in bill now we have to create two things up here so let's call this name one and name two and here we could pass in name 1. if we save this and run it nothing really is going to change because you see it says hello john because in our function here we only use name 1. we could also say and name two now when we do this it's going to say hello john and bill and that's cool now if we leave off something when we pass the stuff originally into our function for instance if we leave off bill we're going to get an error because our function is expecting two positional arguments and we're only passing one so keep that in mind now there are ways around this you can use optional positional parameters and to do that you use brackets you just wrap your thing in a bracket so now if we save this there isn't a name two but name two is optional so our function will assign null to name two so if we run this now it's gonna say hello john and null right so hello john and no that's kind of cool that's what's called a positional parameter because this is the second position i guess you could create a named parameter as well right to do that we use curly brackets but now when we call our function we need to sort of take this variable into account so we need to account for it and say hey for name two use bill right so this will add this to that and this should print out hello john and bill hello john and bill but likewise if we don't pass anything if we don't pass a bill in if we save this and run it let's clear the screen it again says hello john and null so that's cool it's not great you don't want these nulls floating around necessarily unless you want to do logic inside your function then you could test to see if there's a null if there is a null act a certain way if there's not act a different way so you could do that or you could just create defaults so to use a default we come up here and right here when we're defining our function we can say name and the default we'll say is friends right so now we haven't passed in a name two so it will give name two the value of friends so now if we save this and run it it'll say hello john and friends right so that's cool but then down here if we you know did the same thing as we did earlier and said hey have name two bill it will ignore the default and it will assign whatever you've told it to assign into name two so if we save this and run it it should say hello john and bill and sure enough it does and there we go so those are functions in a very quick nutshell very easy with dart as in most programming languages functions are usually a little easier to wrap your brain around than like classes and objects and things that's why most programmers are functional programmers like this not as many class and object-oriented programmers out there because this is just super easy and it does everything you would want it to do so those are functions with dart pretty simple and that's all there is to it in this video we're going to talk about getting user input with dart so allowing the user to type stuff in and then doing something with whatever they typed in so let's head over to our code i'm using the sublime text editor and then get bash terminal as always and as always you can find a link to the code in the pinned comment section below as well as a link to the playlist with all the other dart videos in this series so check that out if you haven't so far so we've got our basic dart starter code that we always have and i'm calling this in dot dart short for input i guess and the first thing we need to do is import a library that will handle input output type stuff in dart so we just come up here to the top and import and it's just from dart we want to import io so that's all there is to that now we can use input output stuff in our program so let's start out by just printing to the screen enter your name now we want to allow the user to enter their name so let's create a variable and there's a couple of ways we can do this i'll talk about in a second we'll just start with the normal way by saying var and then let's name it we'll call it name because we're asking for their name and now we set this equal to s t d i n standard input and then dot we want to read line sync and this is synchronous you could also do asynchronous but we're not going to deal with that in this video we're just going to read line sync so this will take whatever the user types onto the screen assign it to this name variable and now we can do stuff with it so i know we could print hello and then just print out that name let's go ahead and save this and run it see what that looks like so i'm in my c dart stuff directory let's run dart in dart and it says enter your name i can type in john it says hello john so a couple of important things this is going to take in a string right it doesn't take in a variable you can even try you know we could go like int number equals and then you know do this whole thing it's not going to allow you to do that for several different reasons one this just takes in strings two there's some interesting things around nullability here so you know this is one way to create a variable using var we can also define the thing right away so string name equals and then if we copied all this stuff if we try to run this in the normal way we're going to get an error so let's take a look at that if we run this again oh we get an error because you'll see a value of type string question mark which i'll talk about in a second can't be assigned to a variable of type just regular string because string question mark is nullable and string isn't so nullable means can be null like for instance if the user doesn't type in anything and just hits enter that's null well a string can't be null but a string with a question mark at the end of it can right so in this case if you wanted to do it like this we'd have to come back here and just very simply change this to string question mark so if we did that and run it it's going to work just fine hello john right so likewise integer can't be null right you can't have a null number so that's one of the reasons why you can't take an integer uh to use this method but what you can do is take this input it as a string and then convert that string to an integer and we'll probably talk about that in the next video we talk about changing data types but for now this will work just fine just realize this is for strings and for right now strings only but pretty simple and pretty straightforward and really that's all there is to it in this video we're going to convert strings and integers and doubles to each other so if we've got a string we'll convert it to an integer if we've got an integer we'll convert it to a string and we'll do the same thing with doubles so let's head over to our code i'm using the sublime text editor and then get bash terminal as always and as always you can find a link to the code in the pain comment section below as well as a link to the playlist with all the other dart videos in the series so check that out if you haven't so far so we've got our basic dart starter code that we've always got i'm calling it convert.dart so let's just create some variables and we could just call them a b and c and let's set a equal to an integer let's set b equal to a string notice it's in quotation marks and now let's take c and add those two together now normally we couldn't add those two together because you can't add an integer and a string together so we have to convert this string to an integer if we want to add these two together here let's say string to int so to do that we can set c equal to a plus so we need to convert our string here to an integer and we can do that using the parse function so we want to change this to an integer so we call int dot parse and then pass in whatever we want to parse so we want to parse b all right so here if we want to make sure this worked okay let's say a plus b equals c and if this works this should say 40 plus 1 equals 41. if it doesn't we'll just get an error so let's save this and run it head over to our terminal i'm in my c dart stuff directory and let's run dart invert that dart and we see 40 plus 1 equals 41. so clearly our string has been converted successfully to an integer so that's cool it's pretty simple we could do the same thing for string to double let's go var e e and f and let's set d equal to 40 let's set e equal to i don't know 0.1 and again that's a string and let's set f equal to d plus and instead of in dot parse now it's going to be double dot parse and we want to pass in e this will convert our e which is this string to a double so now again we can go print d plus e equals f now if we save this head back over to our terminal run this guy again we see 40 plus 0.1 equals 40.1 which is what we would expect if this string got converted to a double which it did and it added so super simple and it's really all there is to it now we want to convert an integer to a string and this is a little bit different so let's go var g h and i i don't know and let's set g equal to 40. let's set h equal to a string of one and now let's set i equal to now we want to not convert this we want to convert this integer to a string and to do that we use the tostring function so just call our variable we want to convert g and then just go dot 2 string and that's a function and that's it and then we want to add in h so this is 40 and 1. now these are going to be two strings right so if we print this out and let's say g plus h equals i this should be 40 a string plus one a string equals i a string so when you add two strings together they just sort of mush them together they concatenate it right so this should equal 401 we're not doing actual math we're just smooshing them together so if this works if this 40 has been converted to a string we should get you know 401 let's see if that works let's save this head back over to our terminal run this guy one more time and sure enough 40 plus 1 equals 401 because again we're not doing math these are both strings we just smush them together and we get 401 so you can do the same thing with doubles i'm not going to do an example of that so uh you know we could go like this this should be 40.11 if we run this so we head back over here real quick run the sky one more time sure enough 40.11 i'm just going to go ahead and change that back so super easy to convert integers and strings to each other same thing for doubles with dart and that's all there is to it in this video we're going to look at type conversion for user input now a couple of videos ago we did user input so users can enter things right and then in the last video we looked at type conversions switching from integers to strings and vice versa in this video i'm going to put both of those things together because it's not as easy as you might think you would think that you could just take user input and convert it the way we looked at in the last video but that's not the case there's a snafu that we need to talk about and that's we're going to look at in this video so head over to our code i'm using the sublime text editor in the get bash terminal as always and as always you can find a link to the code in the pin comment section below as well as a link to the playlist with all the other dart videos in this series so check that out if you haven't so far so i've got our basic dart starter code i'm calling in in convert.dart for input convert i guess i don't know and of course we're gonna get user input so we know the first thing we need to do is import our dart colon io for input output okay and then let's just come down here and real quick let's just print you know enter a number and then we want to get user input so let's create a variable i'm just going to call it nummy short for number i guess and then we want to s-t-d-i-n-dot read line sync just like we learned two videos ago check that video out if you didn't see it and then normally we could print out you know just nummy and everything is good now if we wanted to print out nummy plus 10 we're gonna get an error let's save this and run it just to make sure head over to our terminal in my c dart stuff directory and let's run dart in convert in convert dot dart and we get a couple of different errors here now the first thing is when you're taking user input using this method it's bringing it in as a string so right off the bat we know we're going to get an error because you can't add 10 to a string it would make it you can't actually add that way so the second problem we're getting is that we look at this real quick we've got this string question mark thing again and remember whenever we're bringing in user input it has to not be null and that's what this string question mark says it allows it to be null or not now you can't add a number to a null right because you know if something doesn't exist you can't add 10 to it because that doesn't make sense it doesn't exist it's null so what we can do is use the double question mark operator which it's the if null operator so basically what it does is it runs a little logic it says hey if it's null change it to something else and that's what we're going to do so let's come up here first let's create another variable and i'm just going to call this nummy do and that's gonna equal nummy one plus ten but of course we know we can't do that so first of all this is a string so we learned in the last video we can convert it with int dot parse and then we can wrap this in like that that will turn it into an integer in theory then we can add 10 but not really because if we save this and run it again we're going to get this same error this same string error oh now we've got now three errors so oh did i forget yeah i forgot all right we need our semicolon all right so all right let's clear the screen now okay we still get one you get all kinds of errors but again that's because we can't convert this because it might be null so how do we fix this again we use the double question mark operator and then we tell it hey if it is null change it to i don't know zero or you could just leave it to blank but we're doing numbers here so let's change it to zero so if somebody doesn't enter anything into the keyboard we're gonna assume that's a zero right so now this will turn it into something that can be converted into an integer this will then convert it into an integer and this will then print it out so we can come down here and we can say i don't know nummy plus 10 equals nummy two and if this works it should you know work all right so let's run this guy again uh-oh all the errors this morning all right so oh numb two nummy two there we go i'll tell ya it's thursday okay enter number 41 41 plus 10 equals 51 and it works let's try it again just to make sure right let's say 100 100 plus 10 equals 110 and we're good to go so again just remember when you're taking in user input it has to account for nullability right because the user might not enter anything it might be null so that's why we use this question mark if null operator basically it's sort of shorthand for logic in this little parentheses right here it's basically saying hey if this is null change it to that right or i suppose you could do it like that too right if it is null it'll change it to zero if it's not null it'll just keep it what it is in our case whatever the user entered if they entered in a number 41 for instance it'll keep it at 41. then this will parse it into an integer to which we can then add 10. so a little complicated not too bad and that's all there is to it in this video we're going to do fizzbuzz in dart and fizzbuzz is sort of a traditional interview question if you go on coding interviews uh it was popular 10 15 years ago i don't know if they're still using it probably not because it's fairly simple but that's what we're gonna do in this video so fizzbuzz is basically a little exercise where they ask you to print out the numbers between one and a hundred if any of those numbers is divisible by three also print out fizz if it's divisible by five also print out buzz if it's divisible by both three and five print out fizzbuzz so you're gonna have a hundred numbers printed on the terminal some of them will have fizz next to them some of them will have buzz next to them some of them will have fizz buzz next to them some of them will just have the number so that's we're going to do using dart so let's head over to our code i'm using the sublime text editor and then get bash journal as always and as always you can find a link to the code in the pin comment section below as well as a link to the playlist with all the other dart videos in this series so check that out if you haven't so far so the first thing we need to do is obviously just create a variable so let's go int and we'll call this num and let's set it equal to one right because we want to print out between one and a hundred so if you wanted to print out the numbers between one and a hundred how would you do that well lots of different ways to do that i'm just going to use a little while loop it's quick it's easy we know how to do it so let's create a while loop let's go while and let's go while num is less than or equal to a hundred now we want to use less than or equal to because we also want to include 100 so less than or equal to and then inside of here we just want to print out num now this is a while loop we also need to increment our counter or else we're just going to get an infinite loop so let's go num plus plus and here let's just say increment the counter all right so let's go ahead and save this and run it just to make sure that worked right i'm saving this as so let's head over to our terminal and then my c dart stuff directory and let's run fizz dot dart when we do we see boom 1 through 100 so all right that looks pretty good so far so good so now let's get into some logic here and let me just get rid of this because we don't actually want to do that yet so we've done logic already we could do an if statement so let's say if and let's start with the fizz buzz category so if a number is divisible by both 3 and 5 we want to print out fizzbuzz well how do we know if a number is divisible well we talked about our operators way back when one of them is the modulus right that returns a remainder so we can say if num modulus 5 equals zero and our double and signs that allows us to make two conditional statements right and num modulus three equals zero well then we want to print and let's just go num let's put a period and let's go fizzbuzz so that should work now we can do else ifs for all the other conditions so we could just knock each of these out so let me just copy this so if num my just 3 equals 0 well that is and let me just copy this thing so we'll have to keep typing this out so if num modulus 3 equals 0 that's fizz right we do another else if num modulus 5 equals 0 then that's going to be what buzz and finally we have else we just want to print out the num right so let's go ahead and save this and run it i think that's kind of all there is to it pretty simple right let's head back over here run this guy again and boom looks like it worked let's spot check this so three that's fizz five that's buzz obviously nine fizz ten bus fifteen is our first fizz buzz it's divisible by both three and five you can divide fifteen by three you can divide 15 by five so we get fizz buzz same thing for 30 fizzbuzz and this looks good spot checking this throughout i think we've done it there we go so pretty simple we've done this in lots of other programming languages this is just sort of a fun little program you would do like i said it used to be an interview question i think interview questions are a little bit harder than this these days but uh back in the day this was an interview question and uh pretty simple now this is not the only way to do this you could use different loops you could do different logic you could do all kinds of different things so give this a try try to do it a different way than this try and do less lines of code maybe make it a little more elegant play around with it see if you can make a better solution with less lines of code like i said and have some fun with it all right guys like i said in this video we're going to start to look at classes with dart before we get started if you like this video you want to see more like it be sure to smash like button below subscribe to the channel give me a thumbs up for the youtube algorithm and check out codingme.com where i've dozens of courses with thousands of videos to teach you to code use coupon code youtube50 to get 50 off memberships all my courses videos and books for one-time fee which is insanely cheap okay like i said in this video we're going to start to look at classes in dart now obviously classes is a gigantic area to talk about and so we're just going to sort of start to talk about it in this video create some very basic classes give you the groundwork of how this works and kind of move on from there so if we head over to our code i'm using the sublime text editor in the git bash terminal as always and as always you can find a link to the code in the pin comment section below as well as link to the playlist with all the other dart videos in this series so check that out if you haven't so far so i've got a file it's just our basic dart starter code and i'm calling it c.dart and what i want to do is create a person class now if you're not familiar with object oriented programming and classes in general just a very very quick overview a class allows you to create an object which is basically a blueprint so you can define some object of reality into a program so if you want to define a person well what does a person have well they might have a name an age a height a sex a address phone number whatever you want to define in your object is your class you can do you can sort of create a blueprint of a person if you want to create a hat blueprint right you know what are hats what what sort of things define hats well the size large medium small the color the shape the whatever so anytime you have a thing you want to sort of map out into a program you can use a class to do that just a very basic overview of a class so to create a class we come outside of our main guy right here and we use this class keyword and then a class name is always capitalized so if we want to create a person it's capital p right so inside of here we can define sort of the road map of what our class is going to map out right so i'm going to say a string and i'm going to make this question mark because you know the nullable thing we talked about a few videos ago and our person is going to have a name it's also going to have a sex let's say and then we also want an integer also not nullable and you know let's call this h so we're just going to keep this person very simple they're just going to have three things name sex and age right very easy so now we need to create a constructor so let's call this constructor and here i'm gonna use the same name as our class right here now you'll notice this is not like void person it's just flat out person and then inside of here we need to define these things that we're going to be passing in here for each person so we're going to have a string for a name sex and then we also have an integer of age right so then inside of here we need to define what we're going to be doing is passing these things so name john sex male age 44. so we need to match those up to these things here so here we just call name equals name sex equals sex and age equals age now not so fast which one are we talking about here name is it this name or is it this name you know how does that work well quite simply this will equal this if we use the this keyword i guess so we need to call this dot for each of these things right so this is basically hey take this that we passed into this constructor here and assign it to this which is this right so these things here are these things up here right okay so strictly speaking this is pretty much all we really need this is sort of our blueprint we've defined what it means to be a person in this program person has a name they have sex and they have an age right well okay that's great but now let's create a method that does something with this information now methods are basically functions we call them functions when they're not in a class when they're in a class we call them methods same thing though right so we can do our void thing here and let's say we want to show data i don't know or show person or whatever let's just say show data and now we can do something so let's just you know print out and let's say name equals name and let's also print out sex equals sex and we also want to print out age equals age right so now we can call this method and it will print out these things for each of our persons persons people whatever okay so we've defined all this stuff now let's actually create one in our program so let's come up here to the top and to create one of these we call our person class and then we name this let's say person one right p1 and that's going to be a person and now inside of here we need to pass in who p1 is right so p1 will be john john is male and he is 44 years old right now notice john male and 44 need to be in the same order as name sex and age right here if we swap these around things are going to get confusing and since we have different data types strings and integers that's going to flummox the whole thing right so okay we've created this now we can do something so let's go p1 dot show data right so this will call this method which will take these things which have been assigned in to this p1 person and it will print them out if we've done this all correctly right so let's go ahead and save this head over to our terminal i'm in my c dart stuff directory and let's run dart c dot dart and we get john male 44 which is exactly what we would expect because that's what's printed out here very cool now we can at any time call anything from our p1 person and we can print out anything from p1 that we want so let's say we want to print out p1.name now we can access any of the things in here so dot name we can access dot sex dot age whatever we want but let's just start with dot name head back over here run this guy again and boom it prints out john very cool instead of doing it like this we can kind of play around with this if we want another way to do this we could say print person's name is and you can do it you can use these brackets here to call name they are same deal here let's just go sex just another way to do this and age years old dot dot dot right so if we wanted to do it like that we could come back over here well we actually still have to come up here and then call p1 dot show data if we did that come back over here around this guy again we see the person's name is john they are male and 44 years old right and we're still printing out john there so anyway you want to do this lots of different ways to play around with this and that's all there is to it now if we get rid of this and let's say get rid of this we can create a second person just as easily as we did the first one so again we'll call person and it's called p2 right it's going to be a person this is going to be mary mary sex is female and mary is say 29 we can at any time we want call p2 dot say show data or we could call p2 dot you know h whatever we want however you want to do this that's how that works come back over here run this guy again actually let's clear the screen and now we see mary is a female she's 29 first his name is mary they are female and 29 years old very cool now this we just printed out p2 we can also print out you know p1 p1 still exists right so if we do both of these clear the screen we get the first one is for p1 which is john the second one is p2 which is mary so this is a massively fast intro to classes with dart but you can see very easy to do this and there's lots of different ways you can set this up this is just sort of one of the standard ways you don't even have to create a constructor if you want you could create another method that sets your data but you know either way it's pretty much the same thing so we just do it this way so not that hard to wrap your brain around like i said classes are a giant topic you could talk for days and days and days on classes this is just a very quick intro on how to start to use classes with dart and that's all there is to it in the last video we created our first class and object but you'll notice we started by initializing it right away with this constructor and this is the code from our last video i'm using the sublime text editor in the git bash terminal as always and as always you can find a link to the code in the paint and comment section below as well as link to the playlist with all the other dart videos in the series so check that out if you haven't so far so very quick recap we created a person class that person has a name sex and age and then you can see here's the constructor that kind of creates an object when we first create this thing if we want to show it we've got this method that shows it right so you see up here we're creating an object p1 that's a person object it's a class right and it's johnmalen44. so we're we're initializing it right off the bat we're giving it data right away that data gets sucked into this constructor gets assigned into p1 and you're good to go well what happens let's get rid of the second instance of p2 what happens if we don't want to start out right away initializing this thing if we don't want to actually give it data right away we just want to sort of create it and then add the data later on perfectly normal you're going to want to do that all the time so i'm going to go ahead and save this as c2.dart it was c.dart before so let's go ahead and save this head over to our terminal i'm in my c dart stuff directory and we can run cdart2.dart and we get an error right too few positional arguments why well it's our constructor here is looking for these things right and we're not passing these things so that's kind of a problem so how can we get around this well very very easy we don't have to use a constructor like this we can just get rid of it so i'm going to change this from a constructor to a method right so let's come down here and instead of just saying hey person let's instead create a method so we go void and let's name this thing let's call this add data or something like that right the rest of this stays the same we're going to pass in each of these things assign them back in like we would before and we're good to go well now you'll see up here we create our person object i'm not going to put any data in here i'm just going to save it like this now this time if we run it last time we got an error this time we don't get an error we get null null and null which is exactly what you want we haven't given it anything we haven't given it any information yet it's just null that's perfect that's fantastic now at any time we can add whatever data we want to p1 and we could do that in several different ways we can call this method right we could call let's say add data so let's go p1 dot add data and then we just pass in whatever we want so john john is a male john is 44 years old right notice you got to keep these in the same order so name sex and age because down here in our method we're looking for name sex and age and it's going to assign those based on the order you pass them in all right so that will work let's go ahead and p1 dot show data just like before go ahead and save this let's come back here and clear this screen run this guy you see the first time when we haven't initialized this haven't added any data we get null null null the second time after we've added the data we get john male and 44 and it works just fine and that's all there is to it now that's not the only way you can add data right we can uh comment that out and we can do it manually so we can go p1 dot and come down here and we have name sex or age so let's go name p1.name and we can set that equal to john right so if we save this and run it we're going to see name is john the rest of them stay the same they're still null and null because we haven't added any data for sex or age we could just as easily come back here and you know do the same thing p1 dot sex equals male and you'll notice we don't have to do these in order we could go p1 dot age and just set that equal to 44. right down here it's name sex and age up here it's name age and sex doesn't matter we're doing them one at a time it's perfectly fine so if we go ahead and save this run it clear this screen boom we get john male 44 just as you would expect to see person's name is john they are male and 44 years old and that's all there is to it so either way it really doesn't matter do it the way we did it in the last video or do it the way we did it in this video it just depends on your program flow what you're trying to accomplish if you have a system to where you want to create an object a person p1 p2 p597 and then later on as those people come in you add them individually you're going to want to do it like we did it in this video if you know right away all of your data and you just want to map it right up you know if you've got a database you're sucking data out of and you're just creating objects as they come do it like we did in the last video you already know your data assign them to the class create an object and you're good to go so either way whatever you're looking for in your program that's how you do it [Music] so that's it you now have a solid fundamental understanding of the dart programming language your next step is to start learning flutter which is an amazing framework for building cross-platform gui apps i have a flutter playlist here on my youtube channel and i'll link to that in the description below so be sure and check that out and also check out my website kodamy.com where i have over 50 other courses on web development database development gui app creation using kinter kv and pyqt5 and much more be sure to use coupon code youtube50 to get 50 off memberships all my courses videos and books for one time fee which is insanely cheap my name is john elder from kodibu.com i hope you enjoyed this course and i'll see you in the next one [Music]
Info
Channel: Codemy.com
Views: 108,154
Rating: undefined out of 5
Keywords: Dart, Dart Programming, Dart Installation, Dart Variables, Dart Data Types, Dart Lists, Dart Maps, Dart Loops, Dart Logic, Dart Functions, Dart User Input, Dart String To Integer Conversiond, Dart Type Conversion User Input, Dart Fizzbuzz!, Dart Classes and OOP, Dart Classes Without Initialization, John Elder, Codemy.com, John Elder Dart, John Elder Dart Tutorial, John Elder Dart Complete Course, Dart Complete Course, Dart Course, dart programming, dart language full course
Id: JZukfxvc7Mc
Channel Id: undefined
Length: 81min 11sec (4871 seconds)
Published: Wed Aug 10 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.