#Google's Flutter Tutorial - SQLite CRUD Operations (coderzheaven.com)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello welcome back to another flutter tutorial in this video I will be showing you how to use SQLite database in clutter so we will be creating tables selecting values from the database I am dating and delete values in the database so let's start and the first thing you have to do is you have to add the SQLite library in the pubs Victoriaville file so I'll be adding the SQLite library I'll be providing the link in the description chart as usual I will be starting with an empty template so the first thing I'm going to do is I'm going to create a new file and I'm going to name the employee door dart so it will be creating a list of employees in the database so let's keep it simple with just an ID and a name and first one is the constructor this takes ID in the name and the next function is called to map that converts an an employee object to a map so happy equal to string and a dynamic and that assigns ID to the incoming ID name to the incoming game and let's return the map alright so the next method will be employed from map so we will be returning an employee object from a map so the parameter will be a map and we are going to extract the ID and the name from the map and it all same priority all right so we are done with the employee class now now let's import the employee and a sink package to our main file and now I'm going to create a new file where we will have our database functions so I'm going to name it DV underscore helper dot dot so let's write the helper class before that we need to import all the necessary packages we need we need the synchronous package io package the path SQLite library the path provider and the employee so let's name the class DB helper so I'm going to declare a variable of type database and let's declare the other database table columns the first one is ID the second one is a name and the third one is a table name so that we will call employee and the fourth would the database name let's call it employee dot DB all right now let's write the functions so first we need to write we need to get the database so get DB and I think so if the database is not equal to null so if it is already in July's we are going to return the database or we are going to create a new database right utilize the database so I'm going to call a new method in your TV so we will be writing that function in your DB that is also an eccentric as well sure let's get the directory where we will have the database submit application documents therapy that will not on the Apple Gibson's Neuromancer a tree now I'll put the documents terror tree your path with the database name all right now let's call the open database with that path and the version let's give it as one and on create of the database we are going to call a new method we'll be writing that mother okay so let's return the database okay now let's write the on treat function so that will take the database instance and the version okay so that will be await DB dot execute are going to write the create table quarry so create a table table with the necessary fields so ID and then ID which is an integer and a primary key and the second one is the name which is a text all right so we have the three table curry now so now we are going to write the number thoughts so inserting so let's write the insert function first so that will return a future employee we are going to call it save so that's going to take an employee object and let's get that the database first so away PB and our insert will return the employee the ID okay so that is we are assigning that to the employ dot ID so insert the table name with employed or to map so that will convert the employ to a map and that will be inserted into the database so let write this in another way with the raw SQL query so this is a way to do it you can call DB Klein the DB instance store transaction which is an a synchronous function and inside that you can have the quarry so variable quarry equal to insert into table and the name with values so name is string so put it inside a court so I have to write it like this okay there we have the insert gory now now call a wait transaction dot insert sorry raw insert with the curry insert curry okay so we are just into the CSIs for information we are going to comment it out and we are going to use the other code okay okay now we are going to do a select from the database so so that will return a future list of employees and the method name is get employees now let's get the database once we have the data piece we are going to create a list of map so that is equal to avoid database client dot query the table name and with the columns that we want it to return so we need the ID and the name okay so we can write this in the raw curry format also so so this is a way to do it Lester maps equal to a weight database client dot raw curry and you can write it like this select start from the table okay we are just counting it out you're not going to use that now so let's get the list of employees so I'm going to declare an empty array of employees and if the maps dot length is greater than 0 so if you have at least one row we are going to look through the maps and we are going to create a list of employees so employs don't add we are going to use the employee dot from Napa third so that will return the employees from employee from each map and add it to the employees array let's return em place the next method will be delete so that will take the employee ID and let's get the database so await database and return database client or delete with the table name and the where argument where we are going to use the IDS the argument and provide the arguments where it's user ID that we are passing right so it will delete the corresponding row from the database now let's write the update method so that will also return the integer and let's parse a employee which is which needs to be updated and get the database as usual as we did in the other other methods and call await it is cleaned out update with a table name and employee which needs to be updated convert that to a map and where will be ID is equal to our apply dot ID right so that I probably need to update ok so we have all the methods now now the final method would be closing the database once you have user database make sure you close it so call closed on the database client that will close the database now now we are going to write some UI to actually see the values that are coming from the database and display it in the UI so for that I'm going to declare a list of employees so that will be a few each other and I'm going to need a text editing controller to control a text field and I'm going to need a strainer name and and in detail it is called current user ID which will be used for updating and deleting okay and I'm going to declare a form key so we'll be having a form which will be having a text field and we'll be using the form to get the value from the text field and a database helper variable and a boolean - to decide the state if it is updating or inserting so on in its state I'm going to create a new DB helper object so we have to import that first let's import that aim for TV helper okay and I'm going to update the he's updating defaults okay now I'm going to return write a mother refresh list so that is going to get the list of employees from the database we are going to call the get employees for third with using the DB helper class so make sure you put it in the set state so that will be rendered in the UI we are going to ID okay so let's write the form so I'll be having a child with a padding and the column so with the center and minimum size and vertical direction down and inside that I'm going to have a text form field so it's for which I will be supplying the controller and I'm going to put a IND name and on validator I'm just checking after it is empty which is empty I'm going to say enter name or not and on saving will be assigning that value to the string name ok so just below that I am going to declare I'm going to have two two buttons two flat buttons so one is going to say update or add so on for us of that button we are going to call a method validator it will be writing and the next was next button we are going to call it cancel so that will reset the update is updating boolean false okay so clear name will clear the text in the text field okay so that's right our validate function so if the funky dot current seat or validate is true so if it if that X field is having some value we are going to save that and if it is updating if the current state is updating we are going to call the update method in the DB helper class for updating we are going to pass the current use ready so we will be assigning that shortly with the name variable okay so called PB helper dot update with the employee object and we are going to reset the he's operating boolean to false okay so then we are going to clear the text field also else we are going to insert the value into the database right insert the employee so I will be now and call DB helper dot save and he's not because that will be auto-generated okay so that's our UI now now we are going to list the employees just below that so we are going to write another water list so that's going to return an expanded widget with a child future builder the future will be the future list of employees that we declared so employees and the Builder will return the snapshot and we are going to check if the snapshot or dot has data if it has data we are going to display the list of employees so that we will write later and we are going to check some error conditions so if it is null or IMD we are going to just display a text saying let's say no records found or no data found okay otherwise we are going to show a progress indicator so if we are having the data we are going to display it in a data table okay so for that we are going to write a method data table and we are going to call it or data table with the snapshot data so write a table we'll take the argument list of employees okay so inside that we are going to return a single child's call view okay with a scowl direction access to a vertical okay and the child which will be a data table so data table will have columns and rows the columns let's declare to column the first one will be named so that will take a text with name label and the second one will be delete so we are going to delete place a delete button in the second column okay so we have two columns now let's write the rows now so rows will take the employees so employees dot map with each employee and convert that to a list because it is expecting a list of budgets okay so each employee will return here data row so he state arrow will have cells so each cell will have a data cell so data cell will have the value displayed so create a text with employ dot name okay now okay we have the list now let's copy that so columns and rows the number of columns and number of cells should be equal so we have two columns and we are now writing the second row so we are going to display a delete button there so on track the delete button we are going to call the delete from the database so DB help add or delete with the employee dot ID right so once you have that we are going to refresh the list in the UI okay okay so let's add the list in the UI alright now let's call that you know uncreate it's okay we have the list now but there is nothing so let's add something okay so let's hot restart so we have it in that it is but today we didn't refresh after adding so go to the add and call the Refresh list there okay so we can put the clear name after and refreshness there okay it's hot we start and add another one okay now let's add John okay okay now let's try to delete okay so that is also working okay so now on top of the row name we are going to set that he's updating to true and set the current user ID to the current employee ID okay now set the text to the employee name in the form field into the text field okay so there we go we have it now now let's try to do it again click on that let's try to update okay click update so that is updater now okay let's try something else I have my name now so I'm going to update my name rip in within click update okay so that is also working so we have all the basic functions now so let's add James and you click it and click with an empty so validation is also working okay so we have the create select delete and update functions now so those are the basic functions that we do in a database so you can have complex queries as well so that's all for this video if you liked the video please don't forget to Like subscribe and share the video and also hit the bell icon for notifications and thanks for watching see you in the next video
Info
Channel: Mobile Programmer
Views: 60,067
Rating: undefined out of 5
Keywords: flutter sqlite, flutter dart, flutter google, flutter coderzheaven, flutter tutorial, sqlite offline database, offline database flutter, mobile programming flutter, the mogile programmer, android tutorials, ios tutorials, google dart, SQLIte, sqlite tutorials, sqflite flutter, database flutter, coderzheaven, mobile programming, the mobile programmer, cross platform, flutter examples, flutter sqlite database, flutter database tutorial, flutter sqflite example
Id: CXyylpd5wqU
Channel Id: undefined
Length: 20min 9sec (1209 seconds)
Published: Sat Mar 16 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.