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

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome back to interpreter tutorial in this video I'll be showing how to save an image as a string in SQLite database and load it back in a grid view so you may have seen my previous video on how to save an image as a string in preferences so let's see how we can do the same thing using the SQLite database okay so let's start so the first thing we are going to need is a model class so we are going to see a photo model class in the DW sender to be back and note the image so let's create the model class photo which has a ID and a photo name and the constructor will accept the ID and put a name and a to map function that will convert an object to a map and return the map so we'll be using these functions Y will be inserted onto the database and retrieve it ok so the second method will reverse the action done by the previous method now we need a helper class for doing the SQLite database functions we need a Singh package ru package also please add the path provider and SQLite plug-in so let's see what those is your flight path provider ok we need the photon model as well plus VP for power now let's declare the database instance and we need some variables for database column ID make sure this ID is same as the model ID ok and name that is photo name ok and we need the table name let's name it photostable next one is database name let's name it photos dot DB now let's write the database functions the person he is getting the DB so if the database instance is not null we will return the database instance otherwise they are going to create a new database instance so we'll be writing a new function in a DB so the first thing is we need that doc mr. free will be able to save the database so get the documents directory and join it with our database name so that it will return the full bath and called open database path version let's give it as one and it's already new method for on create so on create with parameter database and the ocean and call DB door execute create table table name with ID and the name idly name is the photo that made string that we are going to saved so for now let's do it like that so remember that the name is a photo string okay the next method is to save that photo model into the database so DB line dot insert and called photo into map to insert into the table and another method to get the photos so DB client dog curry will return a list of maps which are the records in the database and we are going to check if there if the maps don't length is greater than 0 then we are going to look through the maps and populate the list of photos so photos don't add auto start from map that's I okay I returned the photos good so make sure you close a database as well okay so do you be client dot close okay I don't close the database okay now let's go back to the main file and import the files we need the image picker dot IO utility DP helper and photo also the assing package now we need a future file because the image picker will return a future file and an image the DP helper list of photos okay let me overwrite the init state method and initialize the vehicles images to empty all right PB helper okay now let's write it with her to pick the image from the gallery so called image picker dot Wickham Beach and passing the image source let's set it to image so restored gallery and that's going to return a future file so image file alright so convert that way string to store it in the database so called utility dot basics T for string image file God read a slides sync okay and create a photo model this fast the ID are 0 and the heavy string and can t be help added save ok so that will save the photo string in the database now we need another method to load the images into the images are a so DVR cut out in photos that will return the list of images so let's talk photos so add that to images are a and called refreshing images onto the insert into the database ok now we will show the list of images in a grid view so let's create a grid view which has a padding parent whose axis called to respect her she 1.0 main axis facing off follow and cross axis spacing also for and the children will be the images so loop through the images image the start map which will turn each record make sure to convert this to a list and he cell in the grid view will return an image so called utility dot mates from base64 string so that will return the corresponding image from the string so remember that this utility parties from my previous video so that will decode and encode the data and return the it okay so basic still remains from basics across string will decode the string and return the image okay the link to the previous video is there in the description now let's add an icon to the Appa so I could button I could I can start ad + 1 + or - button we will call the pic image from gallery ok and let's add the grid view as well okay let's reload the app ok select I'll do one thing I will delete the complete app and reinstall it array okay now it's loading okay click the button okay to last for the permission okay it's not shown in the trade video because I forgot to call said state all right okay now it's showing let's select another base okay it's inserted and retrieved back from the database it's called refresh image in the unit state so it will load on the initial load okay let's add some more images okay it's fetching from the database and displaying in the grid view okay all working good okay so it's not recommended to store images in a escalate database because it unnecessarily increases app size so this is just for demo purpose don't do it unless it is really needed so you can have image paths and a database that's my opinion so that's out in this video if you found my videos useful please subscribe to my channel share the video hit the bell icon for notifications also please leave your valuable comments in the comment section below thanks for watching see you in the next video until then bye you
Info
Channel: Mobile Programmer
Views: 22,016
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, sqlite save image, saving image
Id: AZzwLRIBpuw
Channel Id: undefined
Length: 10min 47sec (647 seconds)
Published: Thu Oct 17 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.