6. Save and load the drawing
Save the committed document, not widget state or resolved routes. Add this field and these methods to the State for a storage-free round trip:
dart
String? savedJson;
void saveDrawing() {
savedJson = session.document.encode();
}
void loadDrawing() {
final json = savedJson;
if (json == null) return;
showDecision(session.importJson(
json, expectedRevision: session.document.revision,
));
}Add Save and Load buttons to buildToolbar using these callbacks. This intentionally keeps the saved text in memory; connect the same JSON boundary to your file picker or backend for durable storage.
Observe completed edits
For autosave, register a dedicated listener in initState:
dart
session.addDocumentChangeListener(onDocumentChanged);Add its callback and remove it in dispose, before disposing the session:
dart
void onDocumentChanged(DiagramDocumentChange change) {
final json = change.after.encode();
// Enqueue json in your host's debounced, ordered persistence queue.
// Keep file/network work outside this synchronous notification.
}
// In dispose():
// session.removeDocumentChangeListener(onDocumentChanged);Wire the queue when you choose storage; the callback above only illustrates the subscription. Completed changes include Undo and Redo. A canceled drag produces no completed edit to save. Order asynchronous writes so an older request cannot overwrite a newer revision, and surface storage failures in your application.
Loading after asynchronous work
Capture session.document.revision before awaiting a file or network read, then pass that captured value as expectedRevision. If editing occurred while waiting, the SDK rejects the stale replacement with DiagramRevisionConflict. Let the user retry or choose how to reconcile the content. Do not simply substitute the latest revision to bypass this check.
The in-memory Load above is synchronous, so capturing at invocation is sufficient. For external input, handle decode/validation errors as well as policy denial and revision conflicts.
A new session can open JSON with DrawingSession.fromJson. Supply the same custom registry used to create it. Definitions, action callbacks, host widgets and runtime presentation are not serialized into the document. Reinstall them when reopening.
Checkpoint: Save, move a node, Load. The saved drawing returns; Undo can reverse the accepted replacement. See persistence for codecs and migrations.