Local Database
Explore how to use SQLite in Flutter to persist cart data locally. Learn to integrate the sqflite plugin to create, read, update, and delete data, ensuring that cart items remain saved even after app restarts.
In this lesson, we’ll persist the cart data using SQLite. Using SQLite will require some knowledge of SQL. You can review the An Introductory Guide to SQL course on Educative.
This lesson uses the sqflite plugin to create, read, update, and delete cart items and the path package to manipulate paths.
You will work on the app below:
// Route constants const String productsOverviewRoute = '/'; const String productDetailRoute = '/product-detail'; const String cartScreenRoute = '/cart-screen'; const String settingsScreenRoute = '/settings-screen'; // Prefs Constants const prefStringKey = 'username'; const prefNumKey = 'phoneNumber'; const prefBoolKey = 'theme';
Adding dependencies
Add the sqflite plugin to the pubspec.yaml file. Locate # TODO-1: Add dependency and add the dependency shown below:
Note: Import the dependencies into the necessary files.
Defining constants
In the lib/utility/local_database.dart file, locate // TODO-2: Add field constants and define constants for the database name, database version, and the cart ...