Mastering Local Storage in Flutter: A Practical Guide to SQFlite for Offline Data
The Flutter news you actually need
No spam, ever. Unsubscribe in one click.
Mastering Local Storage in Flutter: A Practical Guide to SQFlite for Offline Data
In today’s world of spotty connections and mobile-first experiences, your Flutter app needs to work reliably offline. Whether you’re building a note-taking app, a fitness tracker, or an inventory management system, local data persistence isn’t just a nice-to-have feature—it’s often the backbone of your application’s reliability. That’s where SQFlite comes in.
Why SQFlite?
SQFlite brings the power of SQLite—a lightweight, file-based database engine—to Flutter. It provides a structured way to store, query, and manage your app’s data locally. Unlike simple key-value stores, SQLite gives you full relational database capabilities: complex queries, joins, transactions, and data integrity. The best part? It’s entirely local to the device, so your app remains functional regardless of network availability.
Let’s build a practical task manager app to demonstrate SQFlite in action.
Setting Up Your Project
First, add the necessary dependencies to your pubspec.yaml:
dependencies:
sqflite: ^2.3.0
path: ^1.8.0
Run flutter pub get, and you’re ready to start.
Database Initialization and Schema Design
The foundation of any database system is proper schema design. Let’s create a database helper class to manage our database lifecycle:
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';
class TaskDatabase {
static final TaskDatabase instance = TaskDatabase._init();
static Database? _database;
TaskDatabase._init();
Future<Database> get database async {
if (_database != null) return _database!;
_database = await _initDB('tasks.db');
return _database!;
}
Future<Database> _initDB(String filePath) async {
final dbPath = await getDatabasesPath();
final path = join(dbPath, filePath);
return await openDatabase(
path,
version: 1,
onCreate: _createDB,
);
}
Future _createDB(Database db, int version) async {
await db.execute('''
CREATE TABLE tasks (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
description TEXT,
is_completed INTEGER NOT NULL DEFAULT 0,
created_at TEXT NOT NULL
)
''');
}
}
Key Points:
- We use a singleton pattern to ensure only one database instance exists
- The
onCreatecallback only runs when the database is first created - SQLite doesn’t have a boolean type—we use INTEGER (0 for false, 1 for true)
- Always include a primary key for efficient data management
CRUD Operations Made Simple
Now let’s implement the core operations: Create, Read, Update, and Delete.
1. Creating a Model Class
First, define a Dart class to represent your data:
class Task {
final int? id;
final String title;
final String? description;
final bool isCompleted;
final DateTime createdAt;
Task({
this.id,
required this.title,
this.description,
this.isCompleted = false,
required this.createdAt,
});
Map<String, dynamic> toMap() {
return {
'id': id,
'title': title,
'description': description,
'is_completed': isCompleted ? 1 : 0,
'created_at': createdAt.toIso8601String(),
};
}
static Task fromMap(Map<String, dynamic> map) {
return Task(
id: map['id'],
title: map['title'],
description: map['description'],
isCompleted: map['is_completed'] == 1,
createdAt: DateTime.parse(map['created_at']),
);
}
}
2. Inserting Data
Future<int> insertTask(Task task) async {
final db = await TaskDatabase.instance.database;
return await db.insert(
'tasks',
task.toMap(),
conflictAlgorithm: ConflictAlgorithm.replace,
);
}
// Usage:
final newTask = Task(
title: 'Buy groceries',
description: 'Milk, eggs, bread',
createdAt: DateTime.now(),
);
final taskId = await insertTask(newTask);
3. Reading Data
Future<List<Task>> getAllTasks() async {
final db = await TaskDatabase.instance.database;
final maps = await db.query('tasks', orderBy: 'created_at DESC');
return maps.map((map) => Task.fromMap(map)).toList();
}
Future<List<Task>> getPendingTasks() async {
final db = await TaskDatabase.instance.database;
final maps = await db.query(
'tasks',
where: 'is_completed = ?',
whereArgs: [0],
orderBy: 'created_at DESC',
);
return maps.map((map) => Task.fromMap(map)).toList();
}
4. Updating Data
Future<int> updateTask(Task task) async {
final db = await TaskDatabase.instance.database;
return await db.update(
'tasks',
task.toMap(),
where: 'id = ?',
whereArgs: [task.id],
);
}
// Mark a task as completed
Future<void> completeTask(int taskId) async {
final db = await TaskDatabase.instance.database;
await db.rawUpdate(
'UPDATE tasks SET is_completed = 1 WHERE id = ?',
[taskId],
);
}
5. Deleting Data
Future<int> deleteTask(int taskId) async {
final db = await TaskDatabase.instance.database;
return await db.delete(
'tasks',
where: 'id = ?',
whereArgs: [taskId],
);
}
Common Pitfalls and Best Practices
- Don’t forget to close your database: While SQFlite handles this reasonably well, explicitly closing your database when your app is disposed is good practice:
Future<void> close() async {
final instance = TaskDatabase.instance;
final db = await instance.database;
db.close();
}
- Handle database migrations: When you need to change your schema, implement the
onUpgradecallback inopenDatabase:
return await openDatabase(
path,
version: 2,
onCreate: _createDB,
onUpgrade: (Database db, int oldVersion, int newVersion) async {
if (oldVersion < 2) {
await db.execute('ALTER TABLE tasks ADD COLUMN priority INTEGER DEFAULT 0');
}
},
);
- Use transactions for multiple operations: This ensures data consistency:
Future<void> archiveCompletedTasks() async {
final db = await TaskDatabase.instance.database;
await db.transaction((txn) async {
await txn.delete(
'tasks',
where: 'is_completed = ?',
whereArgs: [1],
);
// Add to archive table in same transaction
});
}
- Avoid synchronous database calls: SQFlite operations are asynchronous—trying to make them synchronous will block your UI thread.
Wrapping Up
SQFlite provides a robust, reliable solution for local data storage in Flutter applications. By following the patterns outlined above, you can implement a complete offline data layer that supports complex queries while maintaining data integrity. Remember to start with a solid schema design, use proper model classes, and always handle errors gracefully.
The true power of offline storage emerges when you combine it with synchronization strategies for when connectivity returns, but that’s a topic for another post. For now, your app can confidently function anywhere, anytime.
Happy coding!
This blog is produced with the assistance of AI by a human editor. Learn more
Related Posts
Fixing Flutter TextField Issues: Preventing Input Lag, Lost Keystrokes, and Keyboard Glitches
Many Flutter developers struggle with common TextField issues like typing lag, lost keystrokes, or erratic keyboard behavior. This post will diagnose the root causes of these problems, including state management, TextEditingController misuse, and custom decorations, offering practical solutions for a smooth and reliable user input experience.
Choosing the Right Storage for Flutter: Beyond SharedPreferences for Persistent Data
Developers frequently struggle with persistent storage in Flutter, especially when dealing with OS-level backups and app reinstalls. This post will clarify the nuances of different storage solutions like `SharedPreferences`, `path_provider`, and `flutter_secure_storage`, focusing on how to correctly manage data lifetimes (per-install vs. per-user) and avoid issues like device IDs migrating to new phones or data being unexpectedly backed up.
Optimizing Flutter UI Performance: Solving `ImageFiltered` Lag and Smooth Blurring
Flutter's `ImageFiltered` can cause significant lag when blurring large or frequently changing content like text. This post will explain the performance bottlenecks, provide solutions like caching blurred outputs with `RepaintBoundary` or `SnapshotWidget`, and offer practical tips to ensure smooth UI effects without sacrificing frame rates.