Flutter SQLite: Save, Display, Edit and Delete Operations | Complete CRUD Operations | sqflite

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign [Music] we will learn how to configure an sqlite database perform save display edit and delete operations in a flutter app to install the sqlite flutter package visit the pub.dev website copy the dependency task flight and include it in the Project's pubspec.yaml file then click on Pub get I have started a new project and I'm currently working on it create a dart file and name it database helper import two packages s flight and path the path package is used to generate the database path create a class called database helper create two functions one to open the database and another to create the table the open database function should return a database object use the get databases path function to retrieve the path to the directory where databases are stored on the device it returns a feature that completes with the database path use the join function From the Path package to concatenate the obtained database path with the file name my database.db set that path variable to the complete path of the database file call the open database function with the previously determined path specifying the version number as 1 and providing an oncreate callback named create database it returns a feature that completes with a database object if the database does not exist the create database function will be invoked to create the necessary database structure create the create database function which is used as a callback function when creating a database use the execute method on the DB object an instance of the database class to execute a SQL statement the SQL statement creates a table named users with three columns ID as an auto incrementing integer primary key name as a text column and H as an integer column the create table if not exist statement ensures that the table is only created if it doesn't already exist let's create a function for inserting data call the open database method to obtain a reference to the database wait for the completion of open database since it returns a future database the DB variable will hold the reference to the open database create a map object named data that represents the user data to be inserted into the database it contains two key value pairs name and age pass the name and age parameters to the method call the insert method on the DB object an instance of the database class to insert the data map into the users table it returns a future gint representing the number of affected rows ID of the inserted row or -1 if an error occurred open main.dart and import database helper.dart declare Two controller variables for the name and age text fields that we are going to create a method for saving data retrieve values from the name and text fields call the insert user method from the database helper class passing the name and age variables as arguments print the value of insert ID to the console dispose of the two controllers in the body create two text form fields and an elevated button the text fields are for name and age data the elevated button is for saving assign the controllers here call the save data function on the on press of the save button run the app data has been saved 3 is returned this is the third row I had previously inserted two data for testing now let's display the saved data open the database helper file and create the following function open database this function executes a query on the users table using the query method provided by the database class it retrieves all the rows from the users table and returns them as a list of maps where each map represents a row with column names as keys and corresponding values open that main.dart file create the following function get data the method body calls the get data method from the database help Earth class it awaits the result of this method call using the weight and Returns the fetched user data turn this into a void function since there's no need to return anything declare a list variable and assign the fetch data to it declare the list variable globally assign the variable by using set state display the data using this Global variable call the fetch user function inside the initstate method display it after this column therefore it can be wrapped with another column create a sized box with height 30. add an expanded widget then a list view Builder the item count is daedalus.length create an item Builder function pass two parameters build context and index the item Builder returns a list tile the title displays the name field the index is the row index display the H in the subtitle run the app data is displayed after entering the next value it wasn't displayed the display only updates after a refresh let's fix that problem after the insert call the get data function after that update the value set the controller value to null now let's save data it has been refreshed now let's implement the delete function add a delete icon button for that in the database helper class open the file and create a function called delete data the parameter is the ID to delete use the db.delete method to delete it open main.dart create a delete function that calls that delete data and Method from the database helper class call the get data method to get the latest data from the database after the delete call that function in the delete icon button pass the ID parameter run the app delete functionality is working now let's add an edit button and Implement edit functions add an edit button by wrapping the icon button with a row use main access size.min create an icon button with an edit icon create a new dart file for the edit page name it update user.dart import material.dart and databasehelper.dart create a stateful widget called update user create two text editing controller objects for the name and age text fields return a scaffold and create a title create text fields and an elevated button for updating data wrap the column with a padding open main.dart navigate to the update user page using the navigator.push method navigated to the new page now we need to display that particular data here so pass the ID as a parameter open update user.dart and create a user ID variable add it to the Constructor to receive the user ID open the databasehelper.dart file create a function to retrieve a single record using the ID this function returns that single record open update user.dart create fetch data function call that get single data method from the database helper class and assign the data to the text fields call the fetch data function in the initstate method dispose of all controllers values are displayed in the edit page write an update function in the database helper class this is the update function L and create a function to update call the update data function from the database helper class the navigator.pop function is used to return after insertion the parameters are build context and a Boolean value true it is used to refresh the value after updating the main page check if the result is true in that main.dart file when opened in a material page route dot then result if the result is true call a function named fetch data let's write that fetch data function I have done what we did earlier by putting it into a function call the update data function in the update button pass the context parameter run the app change the name and age yes it is updated thank you for watching
Info
Channel: True Coders
Views: 2,743
Rating: undefined out of 5
Keywords: sqlite flutter, flutter save using sqlite, flutter edit sqlite, flutter delete sqlite, flutter sqlite configuration, complete CRUD using flutter sqlite
Id: nQl4uACCYcc
Channel Id: undefined
Length: 17min 13sec (1033 seconds)
Published: Sat Jun 17 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.