 Hello guys in my previous video of current operations on list view and flutter where we have created the contact list I got some comments related to how we can persist this data on shared preferences So let's get started with the shared preferences this time. So I have that project The same we have done in that video. So I will add the link those who have not seen this video and on the top of it we have to Maintain the data in shared preferences So first of all, we need to add one dependency. Let's go to this First back and there is a dependency named shared preferences Just copy this and you can scroll down and check it provides some methods in order to maintain the information So here are the examples given there For example, if you want to keep integer value, you can use the set int method of shared preferences Set bool set double set string set string list And same way for corresponding reading values. They have given this get int get bool get double get string get string list Okay, so let's go back to the project and add the dependency quickly Let me just stop it and we will run it again So here we need one variable to maintain shared preferences. Let's say shared Preferences you can see Okay, so we are not getting it to do what let's go here on the terminal and do write Letter Get again, okay, so let's go here and check for shared Preferences Still not coming Okay, now you can see this shared preferences. So what we need to do is first of all, we need one let's say function over here get shared Preferences this gonna give me the object of shared preferences. So in order to do so I need to make this a sync and I need to await for shared Preferences dot So there is a method get instance So this method is gonna give you what this gonna give you one instance of shared practice So we need one variable on the top. Let's say shared preferences. You can give it any name. Let's say SP and We can initialize it later on so in order to do so just to mention it late and here we can initialize it like this and Once It is initialized. We need to call this in it method and Here we can just call this Get shared Okay, so this gonna initialize your shared preferences and Your SP is ready now Now what we can do we can create a couple of more methods. Let's say We need to get data and we need to set data. So we can say save into SP Okay, and Another method we can have Read from SP Write the code here So whenever we have to save it First of all, we need to remember whenever we save our data it goes to what? So we are keeping it. Let's say on save We are just putting it into The contacts and the contacts is what it is a list So in order to handle the list we need to Save our list into shared preferences So if I go back here and you can see there is one function set strength list But the problem is our list is of object type not of string done So in order to handle that we need to convert it into string type So what we can do here The one way is you can create Here the factory methods So if you're not aware how we can do so What I can do is there is a website here. We have already used it earlier as well app.quicktype.i And here you can do what here you can create your list of objects for example We have name and contact both are string. So we have name and contact both are string You can have any message over here. This is for one object So same way we need multiple objects in array. So I just create one array Paste this object comma paste one more object just and Here what we are getting We need to add comma here comma here and that's it and here what we can do you can specify the names such as contact in our case The name of the class we have given. Okay, and this will create one corresponding Let's select that model for you Okay, so this will create this factory from json and to json method If you're aware about these concepts, then you can do write it at your own Otherwise you can use this and just copy this and Paste it over here. Okay So save this Now your contact model has been updated. We have this factory method So what to do is just go back to the home page Go to this save first and we can do what we can write the code here So once you have to save this into shared preferences, you have to do what? You have to map on your contacts contacts dot Map and for each item what you have to do for each let's say contact You have to do what? Yeah, so you have to encode your So json encode there is one method which will take your object and You can just here we can simply use that Whatever your item that is contact dot. There is a method to To json so we have already created this you can see here To json fine. So this gonna give you each Contact one by one as a string and we can just put it back as a To list Okay, so this is gonna give you a list of strings back Okay, you can see it So this list of strings you can save it into your shared preference. Are you do so? Let's say list of Strings and This would be let's say contact list Spring is Equal to this so you go to your list of Objects as a string now you need to say what your shared preferences dot There's a method set string list and Here you can give any name. Let's say I will give my data and Here you need to pass this list. So this is gonna save your list into This shared preferences permanent Now same way we need to read this as well oops Save this go back here So here we need to read it and in order to read this first of all you need to do what? Just use your shared preferences dot get String list and this should be the same as this in order to fetch data Okay, so the thing is this gonna give you something back that something is a list of strings, which is again optional Okay, so let's go here and just create one variable. Let's say This contact list string I'll just keep the same kind of name because it is local to this you can keep a different name as well Okay, so this gonna give you data back to this So this is optional because there might be null there might be data Okay, so for that we need to write null check over here So if your contact list is not equal to null Only then we have to do what we have to read data. Otherwise not Okay, and same way how to read So use your contact list string dot map as we have used over here This will give you Contact or you can say any item back and Here you have to do what for each item. You have to use that context dot from Jason Okay, so this one I accept one string that you will be getting from Jason dot here we have used in code so we can use The code Jason dot the code and this give you one Data back so here we need to mention that item that is your contact Okay, so once you got this thing simply Just use dot to list as we have used while saving Okay, so this is gonna give you something back. That is what a list of contacts right So we got the list of contacts and you can save it into You can save it into your original contacts. Okay copy this and paste it over So once you have saved it into this you need set state as well so that it can Pre-render everything on the screen and show the updated values So functionality is almost ready What else we left with so we have to write this code over there on the click of buttons So one thing what we need First time once your data is Loaded your application is started. It should read the data from shared preferences. So that means once you're in it is done We are calling what get shared preferences. So once you get shared preferences is initialized We can do what we can simply read the data from shared preferences So that we can display whatever data was previously saved in shared preferences fine This is done Now we need to go to save button where we were clicking on save So here what we need we were checking this If it is not empty, then we were saving it into this contact list And after doing this what we can do we have to save that data into shared preferences as well What to do is simply say save into shared preferences That gonna take this contact list and put it into the shared preferences Fine This is what we are doing. Okay on the click of save button. We need to do that Same way on the click of this update button. We also need to do that so we have this update button and Here we have to do this over here Okay, and same way. Let me cross check. Is it inside the if ES That's fine So both things are done Let's save this and Check this out. Let's say ABC and Just any number Okay, and save this Okay, so we got this error over here this SP has not been initialized So let me just quickly restart it then it should work and Here you go. You can see the data is coming over there Okay, so I think previously saved data is also there. Let me just delete this Delete this one more thing. We have not added any action on the lead so let's do what go to Delete and Here on tap We were removing that contact list and then we have to save that into shear process So I think that is all let's restart it Okay, now this time if I create one more. Let's say ABC and just any number and Just save it now it is coming over here. Okay, let's exit from this app and just run it again contact list and No contact. So actually I restarted the app. So I just need to run it again Because on restart it is not Reinstalling your app. So let's wait for this installation. Okay So now you can see these items are coming over here and let's add one more. Let's say La Okay, what it is La and that's any number And save it Now this time if I exit the app I will run it again now the data should be saved in shared preferences and on Initialization it is restoring the data as we have already done and you can see all the data is maintained But save it is working fine for delete. You can cross-check. Let's delete any of these one or two and Just exit the app And just open it again and that too should not be there as we have removed it from shared preferences And you can see it is working fine. And let's go for this edit as well So for blah, I'll just take it to Good and Save it. Oops. I saved it instead of update. Okay, let's edit this again and Instead of blah, it is saying fine and just update it Now you can exit the app and Again, check it out Okay, so this is all for this video I have shown you how we can save the list of objects into shared preferences and How we can read that? This is the most complicated one in case of shared preferences But if you look at the rest of the methods of shared preferences That are much easier as compared to this Okay if you simply have a list of Items, you can simply save it without conversion if you have simple single string You can simply write it as it is same way for double you can write it as it is for bool You can write it as it is for int you can write it as it is So this example where we have done saving list of objects. We need the conversion Because shared preferences can save the strings only cannot handle objects So that is all for this video if you have doubt do comment in the comment section. Otherwise like share and subscribe. Thank you