Data persistence is a crucial aspect of application development. In Flutter, one of the most common ways to persist data is using SQLite, a built-in SQL database engine. In this chapter, let's explore how you can integrate SQLite into your Flutter application to persist data.
What is SQLite?
SQLite is an SQL database engine that allows you to store data in a local file on the user's device. It's lightweight, serverless, and doesn't require a separate setup. SQLite is ideal for storing simple data and is widely used on mobile devices and web browsers.
Why use SQLite in Flutter?
Flutter is a mobile app development framework that lets you build native apps with a single code base. However, Flutter doesn't provide a built-in way to persist data. So we need to use third party packages like SQLite for this purpose.
How to use SQLite in Flutter
To start using SQLite in Flutter, we first need to add the dependency to our 'pubspec.yaml' file:
dependencies: flutter: sdk: flutter sqflite: ^1.3.2+1
Next, we import the sqflite package into our Dart file:
import 'package:sqflite/sqflite.dart';
With the dependency installed and imported, we can start using SQLite. First, we need to open a database. We can do this using the openDatabase function:
final Futuredatabase = openDatabase( // Specify the path to the database join(await getDatabasesPath(), 'my_database.db'), );
This function creates a database if it doesn't exist and opens it if it does. The getDatabasesPath() function gets the path to the directory where the application can store the user's private data.
Creating tables
Once the database is open, we can create tables in it. For that, we can use the onCreate function that is called when the database is created for the first time:
final Futuredatabase = openDatabase( join(await getDatabasesPath(), 'my_database.db'), onCreate: (db, version) { return db.execute( "CREATE TABLE my_table(id INTEGER PRIMARY KEY, name TEXT)", ); }, version: 1, );
CRUD Operations
With the database and tables created, we can perform CRUD (Create, Read, Update, Delete) operations.
To insert data, we use the insert function:
FutureinsertData(MyData data) async { final Database db = await database; await db.insert( 'my_table', data.toMap(), conflictAlgorithm: ConflictAlgorithm.replace, ); }
To read data, we use the query function:
Future> getData() async { final Database db = await database; final List
To update data, we use the update function:
FutureupdateData(MyData data) async { final db = await database; await db.update( 'my_table', data.toMap(), where: "id = ?", whereArgs: [data.id], ); }
To delete data, we use the delete function:
FuturedeleteData(int id) async { final db = await database; await db.delete( 'my_table', where: "id = ?", whereArgs: [id], ); }
In summary, SQLite is an excellent choice for data persistence in Flutter applications. It provides an easy way to store data locally on the user's device, making your app more responsive and less dependent on a constant network connection. With the help of the sqflite package, we can perform all the necessary database operations easily and effectively.