Skip to content
Blog

SQLite in the Browser: Real SQLite for a Flutter Web App

Run actual SQLite inside a Flutter web app: the official WebAssembly build, IndexedDB and OPFS storage tiers, COOP/COEP threading, and the Dart packages that wrap it.

Published on • November 11, 2026

AI Assistant

SQLite in the browser is not an emulation exercise anymore: the SQLite project ships an official WebAssembly build that runs the real C library in your tab, and the Dart ecosystem wraps it three different ways. What changes on web is not the SQL — it is where the bytes live and which headers your server must send.

The official WebAssembly build

sqlite.org/wasm is the project’s own WASM and JavaScript subproject: the same C code and the same file format, compiled through Emscripten and loaded as sqlite3.wasm. The lineage is long — sql.js, Alon Zakai’s Emscripten port, was the first known SQLite-in-a-browser back in 2012 — but the official build is the one to target now, because it ships the storage backends (VFSes) that make persistence possible.

One catch for Dart: the wasm binary must match your wrapper. package:sqlite3 explicitly warns that sql.js binaries will not load in package:sqlite3/wasm.dart — each package brings or downloads its own build, so do not mix artifacts between them.

Where the data lives

Three storage tiers, in increasing durability:

  • In-memory — fastest, gone on reload. Right for scratch work, previews, and tests.
  • IndexedDB — a VFS that parks database pages in IndexedDB; survives reloads and works even on the main thread, at moderate speed.
  • OPFS — the Origin Private File System, a private per-origin file store with synchronous, byte-level writes designed for exactly this workload.

OPFS is widely available now: Chromium since roughly mid-2022, Firefox 111 and Safari 16.4, both from March 2023, which puts the API in Baseline territory. Two caveats matter. The synchronous calls only work inside a Web Worker, never the main thread. And OPFS is origin-scoped storage — clearing site data deletes your database, and incognito or guest sessions may offer little or no persistence at all, so treat the browser as the owner of that disk.

COOP/COEP: the price of shared memory

The official opfs VFS coordinates its synchronous and asynchronous halves over SharedArrayBuffer, which browsers expose only to cross-origin-isolated pages. That means the server must send both headers with every document:

Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Embedder-Policy: require-corp
# verify with: curl -sI https://your.app/ | grep -i cross-origin

If your host will not let you set them, use the opfs-sahpool VFS instead: it skips SharedArrayBuffer, needs no cross-origin isolation, and is the fastest OPFS option, trading away multi-tab concurrency. Either way, run SQLite off the UI thread — the Dart packages push you toward a Web Worker anyway, and that is also the only place OPFS’s synchronous API is legal.

Three Dart packages, one query surface

dependencies:
  flutter:
    sdk: flutter
  sqlite3: ^3.9.1               # direct WASM bindings
  sqflite_common_ffi_web: ^1.2.2  # sqflite API, web implementation
  drift: ^2.35.4                # type-safe reactive layer on top

sqlite3 gives you the raw engine with an explicit storage choice:

import 'package:sqlite3/wasm.dart';

Future<WasmSqlite3> openEngine() async {
  final sqlite = await WasmSqlite3.loadFromUrlString('sqlite3.wasm');
  final fs = await IndexedDbFileSystem.open(dbName: 'my_app');
  sqlite.registerVirtualFileSystem(fs, makeDefault: true);
  return sqlite;
}

sqflite_common_ffi_web keeps existing sqflite code working. Run its setup once to drop sqlite3.wasm and sqflite_sw.js next to index.html, then swap the factory on web:

dart run sqflite_common_ffi_web:setup
import 'package:flutter/foundation.dart';
import 'package:sqflite/sqflite.dart';
import 'package:sqflite_common_ffi_web/sqflite_ffi_web.dart';

Future<void> openMyDatabase() async {
  if (kIsWeb) databaseFactory = databaseFactoryFfiWeb;
  final db = await openDatabase('my_db.db', version: 1,
      onCreate: (db, _) => db.execute('CREATE TABLE t(id INTEGER PRIMARY KEY)'));
  await db.insert('t', {'id': 1});
  await db.close();
}

SQLite there runs inside a shared worker, storage persists in IndexedDB, and the package is candid that it is experimental: slower than native, one connection per database (singleInstance: false throws for persistent databases), and a plain worker replaces the shared one on Android Chrome, which loses cross-tab safety.

Drift sits on top of sqlite3 with a generated, type-safe API and a backend ladder — shared OPFS, locked OPFS, shared IndexedDB, IndexedDB, then in-memory — chosen at runtime by capability, so the same app degrades gracefully when COOP/COEP or OPFS is unavailable. Already using Drift on mobile? Web is a build flag plus two artifacts (sqlite3.wasm, drift_worker.dart.js) served with the right MIME type.

Backup: take the database with you

Browsers have no “save as” for OPFS, so export explicitly. The sqflite web factory offers writeDatabaseBytes() and readDatabaseBytes(); on the raw API, VACUUM INTO 'backup.db' produces a single file you can stream into a download, and the sahpool VFS exposes exportFile() for the same job. Wire both directions: export on demand or a timer, and offer an import path on first run so a user can restore the file into a fresh profile.

Takeaway: Real SQLite runs fine in Flutter web — pick sqlite3, sqflite_common_ffi_web, or Drift, persist in IndexedDB or a worker-hosted OPFS, add COOP/COEP only if you need the shared-memory VFS, and ship export/import because the browser can delete your database at any moment.

Sources