Must Know Dart List and Map for Flutter App Dev | forEach() map() toList()

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone assalamualaikum so this is a tutorial for list and map in dart for flutter development and in this tutorial i will show you what is list and map and some of their properties as well as when to use them at least two most common cases where we use in flutter okay so without further ado let's get started now here i have this uh this thing here well one thing though i'm not going to use high terminology i'll just keep it very simple so that everybody understands especially the beginners okay now here i have declared a variable it's called my obg map obg and it's wrapped around curly braces and inside we have this thing now two things too important uh and the two two things to take note you see the curly braces in this format okay this is what it makes it a map or map object you can understand it like this the curly braces and key value pair key value pair key value pair so this is this is more like javascript json object okay it's almost the similar the format so to me i understand map is another javascript json object okay json object also has key pair values okay mostly on the left side this is these are the keys and on the right side for each key there is a value okay key means that you can use this to access this value you can understand it like this okay and most of the time mostly the keys are in string format as you can see they could be also also object format okay but most of the time there is strings and now on the right side they could be anything it could be string could be integer or object well in our case we don't have object but you understand the cons concept okay so this two most basic thing makes it like a map so whenever you see things like that you have to understand it is a map based on this curly braces and the key pair value okay that's what makes it map all right so well now we declare it here like this or created this object but there are a few other different ways to create i'll go ahead and create them okay so next one is creating from directly map class okay i'll call it map obj one and this is the map class okay from here you can create it and how about the values and case well you need to fill in them manually so i'll just copy this one over here and i'll put it here and now the key we can have any key name here as we want doesn't matter so i'll keep it name for now and i'll call it assign a value and the same for the second one on the age say it try to use a different age and the third one country china right so this is the second way of declaring a map object okay from using map class not like this okay so first way you can create like this whenever you see things like this you know it is a map object and the same over here if you see it created from new map or the value has been instantiated and has been filled in manually so that's also map object okay now we can verify that they're all the same thing so we could do print over here all right so i'm going to print the first one this one over here let's see the result okay so you see the result is with key and value key and value and at the same time they are in curly braces all right now over here we can print this second one okay so second one is map object one let's print it out as well okay yes so they're same you see the format is same so they're all map object well there is another way to create map object so let's go ahead and create it and this is using pretty much what we did early together actually so let me explain to you okay now over here this string this one and this one refers to key and value so string refers to this one or this these things over here and dynamic refers to the left right side the right side now here we mentioned our keys explicitly to a string and over here we want to do the same thing so that's why we wrote here string and as i said this refers to the right side now right side could be anything string integer object so that's why we have dynamic but if you're sure all of them are objects so you'd write here whatever object you have if you know all of them are integer over here the left side so you can also declare here as int anyway so this is the new map object so what do we want we want our key to be string and value to be dynamic that means it could be anyone it could be string integer or object okay so let's go ahead and fill in them dynamically so that's what i could do here now instead of one would do two okay all right and just to uh be different so let's uh just change the age and things like that say 31 all right now we'll print all of them print so let's see the result okay so this is the first one over here this is the second one over here and this is the third one from here so as you can see whatever the way you create them eventually they have the same format okay whenever you print them or deal with them so internally they have the same format okay it's all key and value pair all right anyway so we'll just stick with the first one over here we don't need this one okay so this is the three ways that you can create any map object there could be more but this three i found all right uh so they're all common okay but we'll we'll stick with this one for now there is a reason why we'll stick with this one we'll see it later down the road okay now here's one thing so map has some interesting properties and functions that we can use and we have to use them uh well just now we use them uh use uh in the printer method right but of course in real life mostly you don't use a print but printer is useful for debugging so in real life if you have map object you want to go over the keys and values using a loop okay so map object if you created a map object it provides some access to some properties so one of them is called 4h so we can access forage now 4-h takes two values the first one is key and the second one is value okay uh well now do remember that the first item here refers to the key second item refers to the value but you can name them anything you want they don't have to be key or value but this is the general convention okay anyway so now over here you can just print the keys all right let's go ahead and run it so we have the keys of course you can also print the values so we'll see uh sorry we are missing a semicolon okay so we'll see our uh keys and values down there okay so that's how you can access them but once again in real life definitely you do something over here based on this value or key okay it's up to you how you use them all right so that's the first way that we can use all right and now there is another way that we can go over them and do whatever we want so map object that's called entries okay well there are a lot of properties that you can use a lot of them make sense when you look at them so we'll see some of the common one that we often use and now entries also has 4h okay now over here it just takes one uh parameter okay like uh you can call it anything in our case we call it entry so now let's go ahead and print entry and see the result okay so it's uh saying it's returning a printing object at the same time it's saying there's a map entry okay now let's see if we can access some of the properties from here so as you can see if you put dot here you'll see key and value key value okay oh by the way if you want to follow along with me uh this with this online editor just to go over here and follow along with me it's an online editor okay anyway so over here so do key so you can print the key values so they're almost similar okay so as you can see here these are the last three because we are just printing keys well now over here uh this entries this part is pretty important why because it provides some useful methods one of them is 4-h and there is another one which most likely use a lot if you develop a flutter app so that's called map okay yeah now map also takes a parameter so anything you want you can pass to it all right and within map you can also print things the way you want okay we do entry dot let's see this time we'll print the values okay now here we'll create a new line object or new line otherwise they're getting messed up together all right now let's run so here so but here let me see i'll just write here new line okay that's all and now let's see something's interesting anyway so you don't you see the new line over here but don't see any printed value right so it's all empty why well now once you use map over here this function dot map what it does it creates i would say that it's in the memory but it's not like list okay which means that it's not indexable okay because the list is indexable but in our case it's not okay so it's not being indexed so that's why it's not returning so you could also understand it's pretty much in a random order whatever it tried to do there inside this map method it created a massive random order so that's why it's not returning anything so you have to make it in random order and in general we use another method is called a to list so what it does this tool list method from this map function while it creates order orderly things or that you can iterate through you can loop through or you can access them using index things like this so that's the basic understanding now regardless whatever it is if you use map if you want to get the value you must use it to list otherwise the values all are in memory but it doesn't get you you want them but it doesn't get you okay so to get the value or to use in other places you must return with two lists and then you would be able to see them let's go ahead and print one more time so slow now here so this is a new line and as you see we printed the values okay yeah so that's what we were able to do that so if you don't have it won't print anything now interestingly enough all of them over here actually you can also use return statement and save the values somewhere you want in this case we'll save it over here in a new variable called new object we'll call it new object and so it's a return statement as you can see because we have equal signs so we'll return something what do you return we'll just return entry and of course after returning we want to print new obg let's go ahead okay now this is pretty interesting now it returns something within this second square brackets as you can see or the third brackets so this is very important um take care of this brackets over here let's look at this which is different than this one right earlier we printed them and it returned things within curly braces anyway we can just return the value i guess so let's go ahead and return the value over here and it will save it in this object or variable and we print it let's see yes interesting right yeah so this is called list okay so far we are doing good so we started with map now we are ending up with list okay of course here as you see we turned whatever happening was there turned it to a list okay so in flatter or dart if you see things wrapped around this uh square brackets or third brackets definitely you should know that they are list okay that's quite interesting right okay uh anyway uh to get a better view first uh i'll print new object and at the same time what we did early i also printed that one and i'll call it map obg equal and uh let's go ahead and print it so there's two so first one within square brackets that's called list the second one is a map so whenever you see things like this you get to know that so it's the list it's the map okay so now we are clear about list and map ideas now regarding list you can also declare your own list all right so i'll get rid i guess all of this we donate them now okay now you can declare your own list so you'll just call a variable list and you can declare whatever you want here so you could say ahmed say 30 say 30 and say china like that okay and now here you can also print list all right so let's see and here we go so so this is for comparison so this is the map new new object new object which is also list type but this is coming from after converting from map object to list object we get this one and this is our map object which you declared early over here printed result and this is the one we manually declared a list okay so what do we understand from here whether you convert your list from a map or you declared your own map they have the same format it's quite important okay and the same for map object okay yeah now this is the basic idea that we learned about map and another thing is that you can also look through list if you want you can use a forage slope or for loop say for where i equals 0 and i is less than less dot length and then you could do i plus plus and then you could do print and the list based on index you can print whatever you want okay let's go ahead and run it so if you have a list uh that's pretty much like you can list through them okay in a for loop so it's more like an array the basic area i think you should know all of you should know what is an array so list becomes more like an array but exactly they're not totally the same but at least on the surface it looks so alright yeah so this is our list now if we can do another thing so here i have this two objects right yeah well uh now this is the map object and this is the list object so what we could do we could create another map object all right and i'll call it map object one and i'll just say here i'll say india all right and i'll get rid all of these things okay so now let's see so these are all map objects so we can let's see map object one okay fine now we can add map objects to a list okay so we'll have word list so we with this we declare an empty list okay now we can add them to list how can you do that we do list dot add it is a method add all right now definitely you could add map obj the first time you this is the first map we added we can also add a second one so we'll just copy paste and instead of doing so we'll add one so now we added two map object in our list now we can print the list and let's see the result okay so as you can see so list has inside map object okay so i understand it more like combination of list and map even though this is uh this is the list object but i understand it like it's list contains map so understanding matters how you understand so now here definitely we can also do for loop here so int i equals zero i less than less dot length and i plus plus now let's go ahead and print our list based on index so let's see what happens okay so we are printing them right printing them based on index all right so now over here let's see if we can access some properties like map sorry name so we are just printing the name so once you have things in a list so and they're in this kind of format you can access using the keys these are the keys in the map object you should understand that so when it comes over here all the maps are inserted into a list now you can access them inside using index and at the same time you can access the properties of the map okay using property you can do a lot more thing anyway so now after saying all of this thing why it's important okay yeah so if you ever developed flutter app you'd see that a lot of time we create a class as a model okay that's what we'll do okay so we'll first declare a class outside of our main function and we'll call it user and say we'll give it some properties string name and end age and the string say location all right now here we'll create our constructor we do this dot name this dot h this dot location yes okay now this is the model we have user class and why we need a model in flutter application if you have data that you want to store in memory most of the time most of the time you want to create a model and use that model to create an object from your data and save it in the database once again in flatter if you have you want to save data in the database so first you want to create a model then you use that model create object and use that object to save in the database or store somewhere it doesn't have to be database you can store somewhere all right so that's why we need it so our model has three properties name age and location so once we save the data to database we can retrieve them and we can retrieve each of the item as we want and we can do whatever we want with them anyway so now i'll go ahead and create an object i'll call up user1 okay and i'll create an object from it so i'll apply name over here say dylan and age 30 and location say shanghai all right okay now we create now with this there is this user object and we can print user object one and we'll see what happens okay now it says instance of user okay now how about if i want to access these values okay can we do that now say dylan try dylan it's this is silly but try it anyway of course you'll get an error okay or you can think uh yeah i have here name so let's try name over here and see now you can't do any of those things so you created the object but you can't use it what's the point you can't retrieve any of the data now here map comes us to rescue okay because now before we save it uh well we created it right yeah and we want to use it okay so before you can go ahead and use it later first you need to convert this data to map object okay remember map object has key and value so right now we actually have the keys over here values these are the values right but we don't have key because we don't have key we can't do things like this user one say key name is name but we don't have here any key what i'm doing here all i'm doing i'm applying or giving some uh values for to the constructor but the values it saves in the memory that it doesn't know how to save it in the memory okay yeah even if it saves you kind of retrieve retreat based on what you don't have any reference so that's why we need key attached with these things okay so you can create object without keys but if you want to use them later efficiently you need keys okay yeah anyway so let's go ahead and create a map object for our constructor or for our model okay but our model let's go ahead and create and it's pretty easy to do that so how do you do that so first you'll call map as we saw earlier that you can use you can create map object three different ways so now here we are creating one and now we want our keys to be string like we saw earlier okay so string now we want our value to be dynamic could be anything dynamic okay yeah so now over here let's see uh we'll call a method method name is you can call it anything but the convention is a lot of them call it json okay so we are calling it json so this is a method and method would return something and what it returned it would return a map object remember we saw earlier that we can create map object of this type okay now here this is a method and this method is a returning map object okay yeah and now it says my key is string and values could be anything okay so now let's go ahead and define the keys all right so well definitely for this one this is a name so we'll apply it as name and how about the value now the value once you pass down from here the values are saved in these variables so for name i'll assign as name and for second one i'll create a key as a name sorry as age and i'll assign the age variable and for this one uh say you want to assign is that a country that it doesn't matter they don't have to have the same kind of name or variable name they look almost same right well they are same but you can have a different key than the variable name itself anyway so we do location all right now now it's returning error here because you need to wrap it around a return statement because it is a return thing right so it's a returning map so now you wrap it around curly braces now it completely became a map object remember we did early at the beginning that map object would be most of the time wrapped around the curly braces so that's what we have now we are returning this okay yeah so we created a method called to json you can call it anything and the purpose of this method is that whatever data you tell supplied to me i'll take the data and turn it into a map object so that you can use it later okay yeah so now so we can do print over here so print user one dot to json okay now let's print it oh i think we have an error okay excellent now as you can see even though i created an object because i convert whatever in the object to a map so i can print them as a map once again and we know that this kind of value is more accessible right yeah so you can create a lot more object it doesn't matter so i'll create object 2 object 3 and i'll just change the location say dhaka and anyway say india all right and well i'm not going to print them now what i'm going to do i'm going to put them in a list variable so what i would do were list i'll create empty list and i'll add list dot add and i'll add user one okay so we'll add all the objects okay so we are adding this user objects to a list okay now definitely as i said that if you really want to use it later you need to convert them to map object but regardless just go ahead and try to print it first let's see what happens okay now as you see just like earlier it becomes the instance but we really don't know what's inside right so that's why we are converting them to a map object using our method or a lot of people call it converting them to json so as i said earlier they're almost similar okay now let's go ahead and print them okay beautiful so as you see we have each of the record and okay sorry so they're all all different pretty much right yeah so shanghai dhaka and india that's what you see all right okay so now now things are being saved in our list variable so that also means that we uh we can print them right we can go through a for loop so let's go ahead and do it so list length and i plus plus so we could do print list i now it would it would print sorry so it would print each of the map object one by one we'll see now here's the interesting thing now because it before we saved it in a list it became a map object right so we can access them using our keys so you just want the names or the countries okay country you just want the country from here so you type in country the key name all right now let's print it out well of course i think it's better as a location because dhaka is not a country shanghai is not a country they're all cities anyway so here i also need to change location let's run it okay perfect so that's what the beautiful thing about list and map so what's happening here let me tell you one more time so if you have data you want the data to be a model and you want to create an object using that model and save it in database or somewhere in your application so before you save it you need to convert it to a map okay or to json object once you do that now you can access them in a list okay so that's how map and list they work together very closely so pretty much if you're using map most likely you'll also use uh a list at the same time so that's most basic idea so now that's one one of the application where we use list and map and another application like for example if you have a lot of json data coming like if you're dealing with server server returns a lot of json data and we know that json format is also like this all right in that case you do the same thing okay but now most of the time in dart if you have json data returning from the server you want to decode it okay so i think there is a method called json decode like that so all you need to do through through your json file over here the loaded json file and it will return it uh as a map object to you like this okay anyway you need to save it somewhere say list one okay so whatever the json data coming from first you decode it i think i'm missing e but anyway uh there is a library i think we need to import i'm not going to do that so in flutter there is this method json decode so the net the network data comes from network and you throw it in the json decode and then it becomes a list okay yeah now once you have this list you can use it whatever the way you want okay hopefully it makes sense so these are the two cases that you use list and map together anyway so if you like the tutorial if you learn something don't forget to subscribe and don't forget to smash that like button thank you so much
Info
Channel: dbestech
Views: 26,822
Rating: undefined out of 5
Keywords: dart list and map, dart list, dart map object, learn flutter list and map, flutter list and map, convert list to map in dart, flutter json to model class, flutter tutorials for beginners, flutter tutorial, dart, flutter
Id: Uz0FgKxXt2o
Channel Id: undefined
Length: 34min 50sec (2090 seconds)
Published: Wed Oct 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.