SharedPreferences in Flutter to Store and Retrieve List of Objects | Loading form Shared Preferences

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys in my previous video of current operations on list View and flutter where we have created the contact list I got some commands 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 were notes in 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 pack and there is a dependency name 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 second method of shared preferences set rule set double set string set string list and same way for corresponding reading values they have given this get end get bull get double get string get stringless 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 here preferences you can see okay so we are not getting it to do what let's go here on the terminal and do right letters public get again okay so let's go here and check for shared preferences still not coming foreign ERS so what we need to do is uh first of all we need one let's say function over here get shared ref erences so this is gonna give me the object of shared preferences so in order to do so I need to make this a sink and I need to await for shared preferences Dot so there is a method get instance so this method gonna give you what this is gonna give you one instance of here practice so we need one variable on the top let's say here's 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 mention it late and here we can initialize it like this and once it is initialized we need to call this init method and here we can just call this get shared reference okay so this is gonna initialize your shade 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 and another method we can have read from ASP point we 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 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 strength 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 a here the factory methods so if we are 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 dot quick type dot i and here you can do word 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 okay 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 name 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 promisation and two Json method if you are 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 list so Json in code there is one method so which will take your object and you can just here we can simply use that whatever your item that is contact dot this is a method to Json so we have already created this you can see here two 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 equipment how do you do so let's say list off strings and this would be let's say contact list ring is equal to this so you got 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 the 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 swings which is again optional okay so let's go here and just create one variable let's say uh 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 no 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 uh you have to use that contacts Dot promotion okay so this we're gonna accept one string that you will be getting from station dot here we have used uh encode so we can use the code Json Dot decode 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 contents right so we got the list of contacts then you can save it into you can save it into your original contacts okay copy this and paste it away so once you have saved it into this you need set State as well so that it can re-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 your init is done we are calling what get shared preferences so once your get shared preferences is initialized we can do what we can simply read the data from share preps 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 share preferences that gonna take this contact list and put it into the shared preference 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 yes 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 delete so let's do what go to delete and here on Tab we were removing that contact list and then we have to save that into shared access 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 uh pre-installing 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 see law okay so what it is love and test 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 for 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 2 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 just change 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 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 first 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 these strings only it 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
Info
Channel: Jatinder Verma
Views: 4,615
Rating: undefined out of 5
Keywords: flutter, shared, sharedpreferences, shared preferences, data persistant, list of objects, storing, retrieving, in flutter, CRUD, CRUD with SharedPreferences, CRUD in SharedPreferences, CRUD with Shared Preferences, jatinder, jatinder verma, verma, insert, update, delete, view, display, start, after
Id: FxnZhygcnHQ
Channel Id: undefined
Length: 18min 32sec (1112 seconds)
Published: Thu Aug 17 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.