Skip to main content

A list of notes: many documents in one app

An app with many notes keeps one document per note, and one storage backend for all of them. The list reads each note without following it; the note page opens one to edit it.

A list of notes: many documents in one appSource ↗

A Flutter app with a list of notes, one document each, kept on the device by Hive. The list reads each title without following the note, a tap opens the note to edit it, and a swipe deletes it.

Flutter nativeFlutter web✓ tested in CI
import 'package:crdt_lf_hive/crdt_lf_hive.dart';
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
import 'package:path_provider/path_provider.dart';

import 'notes_page.dart';

Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();

// On the web Hive uses IndexedDB and needs no path.
if (!kIsWeb) {
Hive.init((await getApplicationDocumentsDirectory()).path);
}
CRDTHive.initialize();
// One backend for the whole app: it holds every note.
final backend = await CRDTHive.open();

runApp(MaterialApp(title: 'Notes', home: NotesPage(backend: backend)));
}

Key points

  • Open one backend for the whole app: it holds every note, and documentIds lists them.
  • One document per note: the document id is the note id.
  • readDocument builds a read-only copy for the list, and you dispose it after reading. openDocument is for the page that edits the note.
  • deleteDocument removes the changes, the snapshots and the identity of a note. A note just left may still be writing: wait until it is closed, then delete it.