Sub topic 11
Embedded NoSQL and Object Stores in Flutter
Prerequisites and the basic idea
This chapter assumes that you know basic Dart classes and null safety, can use Future, async, and await, and can add packages to pubspec.yaml.
An embedded NoSQL store is a database that runs inside the app and saves data on the device. NoSQL means “not only SQL.” Instead of organizing data mainly into tables and rows, a NoSQL store often keeps documents or objects.
An object store saves application objects directly, or stores values that closely match those objects. This can reduce the mapping code required by a relational database such as SQLite.
Common Flutter choices include Hive, Hive CE, and Isar. Their APIs differ, but they share the same general model:
- Open a local store.
- Open a box, collection, or database.
- Read and write values using a key or object identifier.
- Close the store when the application no longer needs it.
When an object store fits
Object stores work well when you usually load an object by its identifier or display a collection of objects. They are also useful for cached API responses, offline-first data, and small-to-medium domain models.
They are less suitable when your application needs many joins, complex reporting, or SQL-compatible queries. SQLite with sqflite or a type-safe layer such as Drift may be clearer in those cases.
| Storage need | Good fit |
|---|---|
| Read one cached object by key | Object store |
| Join several related tables | SQLite or Drift |
| Store a password or token | Secure storage |
| Save a small setting | Key-value preferences |
A small Hive example
Add hive_flutter to the application dependencies in pubspec.yaml. The following example stores maps in a Hive box. A box is a named collection of local values.
import 'package:flutter/widgets.dart';
import 'package:hive_flutter/hive_flutter.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Hive.initFlutter();
final notes = await Hive.openBox<Map>('notes');
await notes.put('first', <String, Object?>{
'title': 'Buy milk',
'pinned': false,
});
final saved = notes.get('first');
final title = saved?['title'] as String?;
print(title);
await notes.close();
}The key is 'first', and the value is a map. Hive serializes the value so it can be restored after the app restarts. The ? checks for a missing value. The cast tells Dart what type to expect.
For a larger application, define a Note class and register a type adapter. A type adapter teaches the store how to convert that class to bytes and back. This gives you clearer types than Map<String, Object?>, but generated adapters add setup and build steps. Isar uses a similar idea with generated collection code.
Common errors and trade-offs
Treating the store like a remote database
Local writes are usually fast, but they still perform input and output. Avoid opening and closing the store for every button tap. Open it during repository or application setup and reuse the open box.
Changing object fields without a plan
Stored data can outlive the version of your Dart class that wrote it. If you rename a field or change its type, older records may fail to decode. Give fields stable names, provide defaults, and write a migration when the library requires one.
Assuming every object store has the same query model
Some stores support indexes and rich predicates. Others mainly support key lookup. Check the package's query and indexing features before choosing it. An index can make repeated searches fast, but it also adds storage and write-maintenance cost.
Exercise
Why might a typed object adapter be safer than storing a note as a map?
Show answer
A typed adapter preserves the model's fields and types, so invalid field names and values are found earlier than with loosely typed map access.