Flutter and SQLite: Building Local Only CRUD Apps

Flutter and SQLite: Building Local Only CRUD Apps

June 23, 2025
Flutter SQLite demo
Flutter, a UI toolkit for building natively compiled applications for mobile, web, and desktop from a single codebase, can be effectively used with SQLite, a lightweight, serverless database, for creating local-only CRUD (Create, Read, Update, Delete) applicationsThis combination allows developers to build apps that function entirely offline, storing and managing data directly on the user’s device. 

1. Setting up the Project and Dependencies:

  • Create a Flutter project: Use the Flutter CLI to create a new project: flutter create my_crud_app.
  • Add the sqflite and path packages: These are essential for interacting with SQLite in Flutter. Open your pubspec.yaml file and add the following dependencies under dependencies: 

Code

    dependencies:      sqflite: any      path: any
Then, run flutter pub get to fetch the packages. 

2. Implementing the Database Helper:

  • Create a database helper class:
    This class will encapsulate all database-related logic, such as opening the database, creating tables, and performing CRUD operations.
  • Database Initialization:
    • Use the sqflite package to open the database connection.
    • Utilize path to define the database file path, typically within the app’s documents directory.
    • Implement a singleton pattern or a similar approach to ensure only one instance of the database is active.
  • Table Creation:
    • Define a method to create tables if they don’t exist.
    • Use SQL CREATE TABLE statements to define the table structure (columns, data types, primary keys, etc.).

3. Implementing CRUD Operations:

  • Create (Insert):
    • Define a method (e.g., insertContact(Contact contact)) to insert new data into the database.
    • Use SQL INSERT statements, providing values for each column.
  • Read (Query):
    • Define methods to retrieve data (e.g., getContacts()getContactById(int id)).
    • Use SQL SELECT statements with appropriate WHERE clauses for filtering data.
  • Update:
    • Define a method to update existing data (e.g., updateContact(Contact contact)).
    • Use SQL UPDATE statements, specifying the WHERE clause to target the specific record.
  • Delete:
    • Define a method to delete data (e.g., deleteContact(int id)).
    • Use SQL DELETE statements, using the ID to identify the record to be removed. 

4. Building the User Interface:

  • Flutter widgets:
    Use Flutter’s widgets (e.g., TextFormFieldListViewFloatingActionButton) to create an interface for interacting with the database.
  • State management:
    Implement state management solutions (e.g., setStateProviderRiverpod, or BLoC) to manage data changes and update the UI accordingly.
  • Data binding:
    Bind the data retrieved from the database to the UI elements, allowing users to view, add, edit, and delete data.
  • Example:

    For a contact list application, you might have a screen to display a list of contacts, and another screen to add or edit a contact. You would use the database helper to fetch the contact data and populate the list, and then use the helper to perform create, update, and delete operations based on user interactions. 

5. Testing the Application:

  • Emulators/Simulators: Test the application on various emulators or simulators to ensure it functions correctly on different devices.