Local-First Architecture

Building Offline-First Flutter Applications with SQLite

By Pawan Kumawat • Published Dec 18, 2025 • 12 min read

1. What is Offline-First Architecture?

An offline-first application treats the local device database as the primary source of truth rather than remote server APIs. All user interactions—creating notes, updating EMI records, or marking tasks complete—write synchronously to a local SQLite database first, ensuring zero UI latency regardless of network state.

2. Setting Up `sqflite` and `path` in Flutter

Add `sqflite` and `path_provider` to your `pubspec.yaml` file:

pubspec.yaml
dependencies:
  sqflite: ^2.3.0
  path: ^1.8.3
  path_provider: ^2.1.2

3. Implementing Database Helper & Migrations

database_helper.dart
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';

class DatabaseHelper {
  static final DatabaseHelper instance = DatabaseHelper._init();
  static Database? _database;

  DatabaseHelper._init();

  Future<Database> get database async {
    if (_database != null) return _database!;
    _database = await _initDB('organizer.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 notes (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        title TEXT NOT NULL,
        content TEXT NOT NULL,
        createdAt INTEGER NOT NULL
      )
    ''');
  }

  Future<int> insertNote(Map<String, dynamic> row) async {
    final db = await instance.database;
    return await db.insert('notes', row);
  }

  Future<List<Map<String, dynamic>>> getAllNotes() async {
    final db = await instance.database;
    return await db.query('notes', orderBy: 'createdAt DESC');
  }
}

4. Summary

Combining SQLite with local repository abstractions guarantees instantaneous application startup and privacy for sensitive user records.

← Back to Blog Index