A text note saved on disk
The smallest app with storage: a Dart program that keeps one text in a local database. Each run reads the note back and adds a line, under the same author. Pick the tab of your storage adapter: the program is the same, and only the lines that open the database change.
A text note saved on disk
A Dart program that keeps one collaborative text in a local database, with the storage adapter you pick. Every run reads the note back and adds a line.
- SQLite
- Drift
- Hive
- main.dart
- Install
import 'dart:io';
import 'package:crdt_lf/crdt_lf.dart';
import 'package:crdt_lf_sqlite/crdt_lf_sqlite.dart';
/// Adds a line to the note stored in `note.db`, and returns the whole note.
Future<String> addLine() async {
final backend = CRDTSqlite.open('note.db');
// Reads the stored note (or starts an empty one), then writes every edit.
final note = await backend.openDocument('note');
final text = CRDTFugueTextHandler(note.document, 'body');
text.insert(text.value.runes.length, 'Hello 🌍\n');
final value = text.value;
await note.persistence.dispose(); // writes what is still waiting
note.document.dispose();
await note.persistence.storage.close();
backend.close();
return value;
}
Future<void> main() async {
// One more line on every run.
stdout.writeln(await addLine());
}
dart pub add crdt_lf crdt_lf_sqlite
Key points
openDocumentreads the stored note before it returns, and keeps the same author (peer id) on every run.- Close in this order: the persistence (it writes what is still waiting), the document, the storage, the backend.
- The database of the adapter may need its own setup on some platforms: see the package it builds on.
- main.dart
- Install
import 'dart:io';
import 'package:crdt_lf/crdt_lf.dart';
import 'package:crdt_lf_drift/crdt_lf_drift.dart';
/// Adds a line to the note stored in `note.db`, and returns the whole note.
Future<String> addLine() async {
final backend = CRDTDrift.open(File('note.db'));
// Reads the stored note (or starts an empty one), then writes every edit.
final note = await backend.openDocument('note');
final text = CRDTFugueTextHandler(note.document, 'body');
text.insert(text.value.runes.length, 'Hello 🌍\n');
final value = text.value;
await note.persistence.dispose(); // writes what is still waiting
note.document.dispose();
await note.persistence.storage.close();
await backend.close();
return value;
}
Future<void> main() async {
// One more line on every run.
stdout.writeln(await addLine());
}
dart pub add crdt_lf crdt_lf_drift
Key points
openDocumentreads the stored note before it returns, and keeps the same author (peer id) on every run.- Close in this order: the persistence (it writes what is still waiting), the document, the storage, the backend.
backend.close()returns aFuture: await it. CRDTDrift.openneeds a native database. On the web, open a Drift web database and pass it toCRDTDrift.fromDatabase, as the Drift documentation explains.
- main.dart
- Install
import 'dart:io';
import 'package:crdt_lf/crdt_lf.dart';
import 'package:crdt_lf_hive/crdt_lf_hive.dart';
import 'package:hive/hive.dart';
/// Adds a line to the stored note, and returns the whole note.
Future<String> addLine() async {
final backend = await CRDTHive.open();
// Reads the stored note (or starts an empty one), then writes every edit.
final note = await backend.openDocument('note');
final text = CRDTFugueTextHandler(note.document, 'body');
text.insert(text.value.runes.length, 'Hello 🌍\n');
final value = text.value;
await note.persistence.dispose(); // writes what is still waiting
note.document.dispose();
await note.persistence.storage.close();
await backend.close();
return value;
}
Future<void> main() async {
// Once per process, before the first open.
Hive.init('note_db');
CRDTHive.initialize();
// One more line on every run.
stdout.writeln(await addLine());
}
dart pub add crdt_lf crdt_lf_hive hive
Key points
- Initialize the database of the adapter once per process, before the first open, as the documentation of the package it builds on explains.
openDocumentreads the stored note before it returns, and keeps the same author (peer id) on every run.- Close in this order: the persistence (it writes what is still waiting), the document, the storage, the backend.
- In a Flutter app the page opens and closes the note: see the Flutter note recipe.