Flutter SQL Database Storage Using Sqlite & Sqflite CRUD

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in flutter if you are writing an app that needs to persist and query large amount of data on local device consider using database flutter apps can make use of sqlr database via the execute flight plugin available on pub. first we will set up sqlite on our project that will create a database then we will create a table and perform the create Rich update and delete features and for the demonstration we will create this simple to-do app do you want to learn flutter in a better and faster way then simply join our 12-week flutter training on heyflatter.com where you master all the flutter topics such as start UI design State Management Firebase clean architecture databases and so on by watching our structured courses that help you for each topic to go from a newbie until an expert level in flutter start by adding the dependencies on your perspect.tml file and get your package under your leave folder create model folder then create this to dot Dart file before creating the table tutorial information Define a to-do color that contains four pieces of data or create a to-do data class model with a unique ID a title A created time and updated time of each to-do then under yearly folder create the database folder then create database service.dart file before reading and writing data to the database we need to open connection to the database and that is what we are going to do on this file first create the database service class and import the necessary packets then create the local variable database of type database of sqlite create the database function and we will check if the database already existed if there is no any database we will initialize our database or we will create our database for that we will create a local initialize function but before that let's create a full pass getter function here we use the join method of pass package which we have imported above to join the given pass which we get from get database path of sqlite which will give us the the default database location of the device and the database name we want to create dot DB then finally create the initialize function here we have received the path from the full path and we will use open database of sqlite which will open a database at a given path then specify the version of the database as the database did not exist we will call encrypt callback function for that let's create a create function which will take two parameters and create a table for the given database using creatable function of 2db class which we will create right now finally on the initialize function written the database or written the created database then under the database folder create to do DB dot Dart file now let's create a table to store information about our to do but first make sure to import all the necessary packets then create a class to do DB and create a table name variable with a given table name first let's create a create table function which we have used it on the database service area here we will create a table if it doesn't exist with the given seven in our case we create a table called tutus based on our to-do data class detail each to do contents ID title created an updated type therefore these are represented as four columns in the to-do step and finally we have used the ID as a primary key finally we have created a database and a table and it's time to read and write data first let's start by inserting data into a table for that we will create a create function which will take a string parameter as a title and here we have used a database where the database service.database function will check if the database exist if the database doesn't exist it will initialize then we have used row insert of sqlite which will execute Arrow SQL in search query and Returns the last inserted row this function will create a to-do with a given title and with the current date and time with the local time zone of the device we have created our database and tables now let's see how to fix how to use from the table or the database first start by fetching all the to-do's for this we created fetch all what we are doing here is we execute Arrow SQL query to fetch all to this from the database after this we mapped the fetched tutus to our to-do dark model to map our fetched to this to our Dart model we will create this Factor emitted on our to-do model class which we have created earlier so create this Factory method with the given details like this whenever you want to fetch one to do from all the to-do tables we are going to replace it by its ID so for this let's create this method Page by ID here we execute Arrow hql query to fetch a to-do which are this exact given ID from our to-doos we have seen how to create and fetch to this from our database now let's see how to update our introduce first create this update method we used updates from the HQ lab package which will update rows in the database and return the number of changes made here we are going to update a to-do with the given ID and change or update its title using their given ID pass to the method and here we have specified an algorithm to use in case of conflicts here where is an optional where clue to apply when updated and the power signal will update all rows and here we have included this glue which will be replaced by the value from this property finally let's see how to relate our tools from our table for this we have created this delete method which will take an ID as a parameter and here we have used row delete of sqla package which will execute Arrow SQL delete query and return the number of changes made under your leave folder create this patch folder then under the page folder create this to do space.dart file then create this to those page stateful widget and create these two variables then create this fetch to this method which will fetch all our tools from our database and call it inside our init State method on our build method we return it as scaffold and we used our Floating Action button to create new to-do's for that on the on-pressed property of our Floating Action button we will call show dialog and on the Builder we used create to-do widget a widget which we are going to create right now before creating our widget under your lip folder create widget folder then under the widget folder create to the widgets.dart file then inside the file create this stateful widget and create these two final variables then initialize the widget like this on our build method first we created is editing a Boolean value which we are going to use it later then written an alert dialog widget on the title here we are going to use is editing a Boolean variable which we have created here we are going to check whether the user is editing or adding a to-do then on the content property of the alert dialog we use the form and on the child properties of a form here we used a text form fill here we are validating and decorating the text form field then inside our alert dialog widget we created text buttons then back to our to-do switch file all our Floating Action button on the on press which we have called the show dialog and on the build on the Builder properties of our show dialog we have called create 2D widget a widget which we have created then on on submit property of our widget we will call our create method which we have created earlier and we will find the title then we will call all our toodles to show for the user and we'll navigate back on our body properties of a scaffold written feature Builder and on the Builder first we check the current state and we will return circular progress indicator and we'll pass our snapshot data to our to this variable then we check if our to do is empty if r2dus is empathy or the user have finished or deleted or the toodles then we will return a text no to this but if the to-do's are not empty we will return a list view here on the item Builder we created a final to do variable and assigned to this currently indexer to do to it and we created a final subtitle variable then parse it and assign the current to use updater type or Creator time then on the list view we returned it Elite Style on the list style title property we Ascend the currently to the title property then on the subtitle will send the subtitle which is the currently updated time to delete any to-do we added an icon button on the trailing properties of elite style here we use the delete icon and on the unpressed property of the icon button we called a delete method which we have created and passed the currently indexer to-do ID to update while a user tap on any tutus or any list tile Azure dialog will pop up and on the Builder properties of azure dialog we have called create to-do widget a widget which we have created earlier and we have put the currently indexer to do or to do variable 2 to do property of the create to-do widget then on the submit property we have called an update an update method which we have created in the past the currently indexed ID and title then finally we fetch all to those and now navigate back and finally let's run our upper Let's test it as you can see on the screen as we are not adding any to this before it is returning us no to the stack start by adding a to-do's for that spread The Floating Action button here add your first to do in our case we have added a to-do one then press OK as you can see here we have added it to do one and let's add a multiple to this for the demonstration and we have added three more tutus so let's test the delete future to delete any to this what we are going to do is to press this delete icon Button as you can see the 2d4 is deleted and let's see the update feature for that tap on the to do second and let's call it to do 24 and add 4 and press OK as you can see to do two is updated 2 to do 24. from this you can delete all the tutus and Bug to note reduce foreign
Info
Channel: HeyFlutter․com
Views: 38,493
Rating: undefined out of 5
Keywords: android, crud operations, flutter, flutter database, flutter local database, flutter local db, flutter persistent data, flutter sqflite, flutter sqflite crud, flutter sqflite database, flutter sqflite tutorial, flutter sql, flutter sql database, flutter sql tutorial, flutter sqlite, ios, local database, mobile, open database flutter, sqflite, sqflite crud, sqflite flutter, sql database, sql database engine, sql db, sqlite, sqlite flutter, sqlite flutter tutorial
Id: 9kbt4SBKhm0
Channel Id: undefined
Length: 9min 55sec (595 seconds)
Published: Tue Jun 20 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.