VOOZH about

URL: https://www.geeksforgeeks.org/flutter/sqlite-in-flutter/

⇱ SQLite in Flutter - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

SQLite in Flutter

Last Updated : 23 Jul, 2025

SQLite is a very popular choice of local storage database. It is an SQL lightweight and serverless SQL database engine that is highly efficient and user-friendly. Flutter, Google's UI toolkit for building natively compiled applications, doesn't come with built-in support for local data storage but provides robust support for integrating SQLite databases. Therefore, SQLite can be easily integrated into Flutter projects to store and retrieve structured data locally.

In this article, we will get to know how we can integrate and use the SQLite database with the Flutter project, Stepwise with an example of the user management database of GeeksforGeeks.

Prerequisites

Before we dive in, there are a few things you'll need to have:

  1. Basic knowledge of Flutter and Dart
  2. Flutter SDK installed
  3. Dart SDK installed
  4. IDEs such as VSCode & Android Studio

Having these prerequisites in place will set you up for success as we move forward. So, make sure you have them ready before we get started.

Steps to Integrate SQLite Database in Flutter

Step 1: Create a new Flutter Application

Create a new Flutter application using the command Prompt. To create a new app, write the following command and run it.

flutter create app_name

To know more about it refer this article: Creating a Simple Application in Flutter

Step 2: Adding Dependencies

To get started, open up your pubsec.yaml file in the project structure. Now, you'll want to add the following dependencies:

dependencies:
flutter:
sdk: flutter
sqflite: ^2.4.2
  • is the package that provides SQLite integration for Flutter.


Now, run the command below in the terminal.

flutter pub get

Or

Run the below command in the terminal.

flutter pub add sqflite

Step 3: Defining the User Model

Create a file in the 'lib/user.dart' to define a model class to represent user data. Here's an example of a simple gfg user model class that has a user ID, a username, and an email, along with a constructor that initializes the data members :


Step 4: Defining the Database Class

In the Database helper class, we have all the functions implemented here, The Database class has the following methods

  1. initDb(): The function is used to initialize the database. It checks if the 'gfg_users' table exists, and if it doesn't, it creates it.
  2. _onCreate(): It is a callback function that gets executed when the database is created. Its purpose is to define the schema of the 'users' table.
  3. insertUser(): To insert a new user into the 'gfg_users' table.
  4. queryAllUsers(): Retrieves all users from the 'gfg_users' table.
  5. updateUser(): To update an existing user in the 'gfg_users' table.
  6. deleteUser(): Deletes a user from the 'gfg_users' table by their ID.


Step 5: Defining the main file

  • Initialization: We make sure to initialize the database and insert the users before running the MyApp widget.
  • User List Display: The UserList widget is responsible for displaying the list of users that we fetch from the database using the DatabaseHelper class.
  • State Management: To keep things organized, we use the initState method to fetch the users when the widget is first initialized and update the user interface accordingly.
  • User Display: We show each user's username and email using a ListTile widget within a ListView.builder.

Output:

👁 sql_flutter


Thus, by following these steps, we can integrate SQLite in a Flutter project.

Comment
Article Tags:
Article Tags:

Explore