Persist Data with SQLite: Using SQLite in Flutter for Easy Saving
Introduction
If you're building a Flutter app that needs to store and retrieve large amounts of data on a local device, using a database is the most efficient way to handle this. Flutter offers several options for local data persistence, but one of the best choices is SQLite.
SQLite provides fast read and write operations compared to alternatives like files or key-value stores, and it’s great for structured data. In Flutter, we can integrate SQLite using the sqflite
plugin.
In this guide, we’ll walk you through the basics of working with SQLite in Flutter, covering how to create, read, update, and delete data in a database. We’ll use a simple example of storing information about dogs in a database. By the end of this tutorial, you’ll be able to use SQLite effectively in your Flutter apps.
Steps Covered in This Tutorial:
Add dependencies.
Define the data model.
Open the SQLite database.
Create a database table.
Insert data into the database.
Retrieve data from the database.
Update data in the database.
Delete data from the database.
Add the Dependencies
To begin, we need to add the sqflite
and path
packages as dependencies in our Flutter project. The sqflite
package allows us to interact with the SQLite database, while the path
package helps in defining the correct file path for storing the database.
To add these dependencies, run the following command in your terminal:
Make sure to import the necessary packages in the file where you’ll be working:
Define the Data Model
Before creating the database table, we need to define the data model. In this example, we’ll create a Dog
the class that represents the data structure we want to store. Each Dog
will have an id
, a name
, and an age
.
Here’s the code to define the Dog
class:
This class will help us easily insert, query, and manipulate data in the SQLite database.
Open the SQLite Database
Next, we need to open the SQLite database before performing any operations. This is done using the openDatabase()
function from the sqflite
package. We’ll also use the getDatabasesPath()
function from sqflite
and the join()
function from the path
package to ensure that the database is stored correctly on the device.
We’re now ready to start interacting with our database.
Create the Dogs Table
Create the Dogs Table
After opening the database, we need to create a table to store the data. For this example, we’ll create a table named dogs
, which contains three columns: id
, name
, and age
.
Here’s how you can create the table:
This CREATE TABLE
statement defines the structure of the dogs
table, and now our database is ready to store data.
Insert Data into the Database
To insert a new dog into the database, we need to convert the Dog
object into a Map
and then use the insert()
method. Here’s a function that performs the insertion:
Here’s an example of inserting a dog named "Fido" into the database:
Now, the dog is stored in our SQLite database.
Retrieve Data from the Database
To retrieve a list of dogs stored in the database, we can use the query()
method. This returns a list of maps, which we can then convert back into a list of Dog
objects.
To print the list of dogs:
Update Data in the Database
If you want to update an existing dog’s information, you can use the update()
method. This requires specifying a where
clause to make sure you update the correct row in the table.
For example, to update Fido’s age:
Delete Data from the Database
To delete a dog from the database, use the delete()
method, passing the dog's id
as a whereArg
to ensure you delete the correct record.
To delete Fido from the database:
Full Code Example: Using SQLite in Flutter
Here’s the full code for creating, inserting, querying, updating, and deleting data from an SQLite database in Flutter using the sqflite
package. In this example, we’ll store information about dogs in the database.
Explanation of the Full Code:
Dog Class: This class represents the data structure that will be stored in the database. It includes fields for
id
,name
, andage
, and a method for converting the object into a map.Database Initialization: The
openDatabase()
function creates or opens a database at a specified path and creates adogs
table if it doesn’t exist.CRUD Operations:
Insert: Adds a new dog record into the
dogs
table using theinsert()
method.Query: Retrieves all dog records from the database using the
query()
method and converts them into a list ofDog
objects.Update: Updates a dog’s details by using the
update()
method and specifying theid
.Delete: Deletes a dog from the database by its
id
using thedelete()
method.
Example Usage: In this example, a
Dog
object namedFido
is created and inserted into the database, his age is updated, and then he is deleted from the database, demonstrating all CRUD operations.
Conclusion
Using SQLite in Flutter with the sqflite
plugin gives you a robust, efficient way to store structured data locally. With this full code example, you now have everything you need to get started with SQLite for your Flutter apps. You’ve seen how to create a database, perform common CRUD operations, and manipulate data.
If you want to take your app to the next level or need professional assistance, consider Blup for custom app development and advanced Flutter solutions. Our team is here to help you build powerful and scalable Flutter apps with optimized data persistence solutions.