Sembast: A Pure-Dart NoSQL Database for Offline-First Flutter Apps
Store, query, listen to, and encrypt document collections in Flutter with Sembast's pure-Dart, cross-platform database API.
Published on • November 2, 2026
AI Assistant

Offline-first apps need persistence that behaves identically on mobile, desktop, and web from one codebase, and Sembast (3.9.0 by tekartik.com) delivers exactly that: a NoSQL persistent embedded file-system document database for the Dart VM and Flutter that is 100% pure Dart — no plugin, so it runs everywhere including web (pub.dev).
Opening a Database File
The whole database resides in a single file, is loaded into memory when opened, and every write is appended with automatic compaction. Open it with databaseFactoryIo plus a path_provider path, then swap in databaseFactoryWeb (sembast_web) or sembast_sqflite — the rest of the API is unchanged:
Future<Database> openAppDb() async {
final dir = await getApplicationDocumentsDirectory();
final path = '${dir.path}/animals.db';
return databaseFactoryIo.openDatabase(path);
}
StoreRef, CRUD, and Auto-Increment Keys
A StoreRef is a named collection. intMapStoreFactory.store('animals') keys records by auto-increment int with Map<String, Object?> values; stringMapStoreFactory does the same with String keys.
final animals = intMapStoreFactory.store('animals');
Future<int> addAnimal(Database db, String name) =>
animals.add(db, {'name': name, 'legs': 4}); // mints the key
Future<void> renameAnimal(Database db, int id, String name) =>
animals.record(id).put(db, {'name': name, 'legs': 4});
Future<Map<String, Object?>?> animal(Database db, int id) =>
animals.record(id).get(db);
add creates a new record, while record(id).put creates or replaces a known key.
Transactions Are All-or-Nothing
await db.transaction((txn) async { // pass txn, never db, inside
await animals.add(txn, {'name': 'axolotl', 'legs': 4});
await animals.add(txn, {'name': 'okapi', 'legs': 4});
await animals.record(1).delete(txn); // throw to roll it all back
});
Every write inside runs atomically: one thrown error rolls back the entire block.
Querying and Reacting to Changes
final finder = Finder(
filter: Filter.and([Filter.greaterThanOrEquals('legs', 2), Filter.matches('name', '^a.*')]),
sortOrders: [SortOrder('name'), SortOrder('legs', false)],
);
final snaps = await animals.find(db, finder: finder);
final sub = animals.query(finder: finder).listen((s) => print('${s.length}'));
await sub.cancel();
The same Finder drives update, delete, and count, and the query stream re-emits only when matched records actually change — ideal for a StreamBuilder.
Encryption and Gotchas
- Encryption is a user-defined
Codecpassed ascodec:at open time, so it works identically on every backend, including web. - Derive the IV deterministically from your key: a fresh random IV per launch makes existing records unreadable.
- The database is fully in memory: great for tens of thousands of small records, wrong for blobs — store files on disk and keep only their paths.
- Hold one
Databasefor the app lifetime andawait db.compact()after bulk deletes, since deletions are appended rather than reclaimed. - Choose Hive for tiny key-value blobs, sqflite/Drift for SQL and joins, and Sembast for portable document collections with queries.
Sembast gives you a real document database — stores, queries, transactions, live streams, optional encryption — in pure Dart that runs on every Flutter target.