 Hello guys in my last video of shared preferences in Flutter in order to store and retrieve the list of objects I got one comment related to how to implement that undo functionality So please make a one short video about this point how we can do the under functionality So let's get started go back to the code go back to the code and This is the project we have done earlier So I have already told in the comments that you need to maintain one more variable in order to maintain the last deleted item so what we can do here is that's great Couple of variables the one would be of holding contact that would be let's say recently deleted contact That would be initially null. So what we can do we can just make it nullable Okay, then the second thing we need is we need to maintain the index of that Element whosoever is deleted so that whenever we do undo it should come on the same place. Okay So let's do what int The deleted index so initially I will just make it minus one Okay, and this is what we need to do now next is we need to show the snack bar So for that what I will do is I just create one separate method that is show snack bar with Or you can say with undo Okay So here we will be writing the code so what we need to do first of all we need to show the snack bar So for that we will be using scaffold messenger Here it is Dot there is a method off which will get context and with that we can say show snack bar So once you do this, this will expect one snack bar object. Let's create that as well snack bar and Here it is asking for context and The context would be your any widget. Let's make a const text and The text would be let's say deleted by mistake Let me reformat it So this will be asking for the content. So it is displaying now. We have to add action button over there So for that we have action Attribute so let's use that action and use snack bar action So here it will be asking for one label. Let's give it undo and On press we need to implement. So what to do once user click on undo Okay, let's go back. I just missed this So here we need to write the code what to do when user press undo So let me show you first of all we need to do what insert We have to insert back the deleted or you can say last deleted So the last deleted we need to insert back into this Contacts list where we are displaying all the contacts. Okay, so let's do what? right here last deleted to Contacts This is one thing we need to do once it is done. You you need to just read the state so that it can reflect on the UI part and Once you roll back that recently deleted or last deleted you have to keep it into shared preferences as well so save into SP okay, so how to do that so first of all We need to say what? contacts Dot if you do add So it will add at the end So we want to do what we want to insert at specific location for that we can use this insert method so here we need to Enter the index that would be recently deleted index so we will be updating that as well and here you have to Specify that last deleted item that would be stored in the recently deleted contact Okay, so this recently deleted contact is a basically your Optional type so we can do what we can simply mark it This so this will make sure that it will always give the value So this is done. So we have inserted back the deleted element now You need to do what to rebuild the state so for that we can simply write set state And then we need to save into shared preferences for that. We already have the method save into SP so this part is done over here and Let me cross check what I missed. I Think I missed one packet Okay Okay, so this is all for this undo functionality and What we have to do is on the click of the lead we have to show the snack bar So let's go to the delete and Here what we are doing. We are just removing a particular entry at This click of the lead So what to do is just before that you can simply say recently deleted contact should be what whatever contact and The element whatever it is so from that contact list We are getting the element at particular index that we are keeping it in the recently deleted contact And same way we need to maintain that recently deleted index as well. So that would be simply index So this is fine Okay, let me just Okay, so this will update my values over there and We need to do what after deletion and we are saving it and we need to show that snack bar so Show snack bar with undo. So that is all we need to do So let me just stop it and rerun it because to cross check I need to Stop the app and run it again. So these changes are on the go So it will not be reflected until and unless I reinstall the app. So let's wait Okay, so let's go back to the application and let's try to delete this Monica and you can see this undo if I click it you can see your Monica back So let's remove this and the run it again and cross check. Are we having that Monica? Okay, so it is here. So let's try to delete this Monica now and We are not doing undo this time So let's close the app and run it again And you can see that Monica has been removed. Let's delete this and undo this and just Exit it and run it again So you can see now the good is still there So that is all for this video if you have any doubt do comment in the comment section. Otherwise like share and subscribe. Thank you