Google's Flutter Tutorial - Save Image as String in Local Storage- Preferences (coderzheaven.com)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome back to on the flutter tutorial in this video I'll be showing how to save an image as a string in preferences and load it back so first thing we need is the image picker plug-in I'll be providing the link to all plugins in the description so let me import that they made speaker plug-in and we also need the dot io package ok now we are going to write a function to pick the image from the gallery so pick image from gallery and passing the image source it can be gallery or a camera whatever you want the image bigger is going to return a future file ok so image file is equal to image picker dot pick image and pass in the source ok now we will write a function to show the picked image in the UI so routed it means from gallery so that's going to return a future builder type file on the future is the image file so when it is returned this will be triggered and a cig snapshot file snapshot with the pill context okay so I am going to check if the snapshot dot connection state is equal to connection state or time so if it is done I am going to check if there is something in the data there is nothing there going through it on a text widget saying error so otherwise we know that there is something in the data so here we start file and pass in the snapshot or data okay next we are going to check if there is some error so if there is some error to return a text with Ted and say error picking image okay hyaline into a center okay let me align the other one also to Center okay otherwise nothing has loaded till now so we're going to say no image selected okay so I line this to Center as well okay now let's add the image to the main UI the build method okay you know we need it invoke the week a maze from gallery method right so let's add a icon button to the app bar so I can start add an odd plus odd function we will call the piggy beans from gallery it may service let's set it to gallery so run the app I'm going to reload the app okay select an image okay so the header here he is I forgot to call the set state or the image file okay so there you go okay the image is loaded now so the next thing we are going to do is we are going to save this in the preferences as a strength so let me write a utility function and we are going to need the shared preferences plugin okay import the plug-in and we are going to need the material package because we are going through turn image from here itself okay and a key so this is the key at which we are going to save the image in preferences so image saved image to preferences method will save the image as a string into the preferences so get the instance of shared preferences and call preferences dot said stream pass in the tea he's in whiskey and the value value and the next method is to get him a string from preferences so get image from references so that's going to return the image string so shared preferences dot get instance and called preferences dot get string and passing the key okay now we need one method to convert that amazed to a base64 string ok so pass in the whites and this function the base64 encode will convert the bytes to a string we need at the function to convert the convert the string to mate okay so image from basics you go string and pass in the string that's going to return an image and call base64 decode then fitties walks with dot fill okay now we need to go back to a main file and call these functions from there okay so import the utility and so when the image is returned we are going to call utility dot save metric references and convert that to a string okay now we have saved the image of the string right so we need at the water to load the image back from preferences so let us write a method load from loading means from preferences and call utility dot get image from preferences that's going to return the image string so if it is null we are going to return otherwise we need an image variable so image image from preferences so amazed from preferences is equal to utility dot it mates from basis repo string image okay so that's going to convert this string back to image now let's add one more button to the a bar to call this function alright if the image preferences image from paralysis is known we are going to show an empty container otherwise show the image okay don't forget to call church-state all right okay reload that since icon okay you got that okay it's loading from the preferences okay okay let me reset the image when we click on the plus button okay that's working okay cool so that's how we can save an image in preferences as a string and load it back so I will be doing another video on how to save the image as a string in a SQLite database and load it back in a grid view so that's all in this video if you liked the video please don't forget to Like subscribe and share in the Baloch and for notifications thanks for watching and see you in the next video bye
Info
Channel: Mobile Programmer
Views: 25,583
Rating: undefined out of 5
Keywords: Google flutter, flutter save image, flutter preferences, google dart, flutter tutorial, android tutorial, ios tutorials, dart tutorials, cross platform, shared preferences, mobile programming, mobile programmer, example, code, android examples, save image as string, local storage flutter, flutter dart
Id: 2X_TCU2n47w
Channel Id: undefined
Length: 9min 5sec (545 seconds)
Published: Tue Oct 15 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.