📚 Encyclopedia

Flutter & Mobile · updated 2026-10-13

Autosaved drafts that come back from the dead (lifecycle pitfalls)

flutterdraftsautosavedisposelifecycleshared-preferencesdebugging

A composer that autosaves drafts (debounced to local storage, saved again on dispose, cleared on submit) looks simple and hides three races. TLDR shipped two separate fixes for "a post I already sent came back as Draft restored". The lessons apply to any form with autosave.

Hole 1: the in-flight submit

The submit handler did: create post, if (!mounted) return, a few more awaits (fetch, metadata, reminders), then mark submitted and clear the draft. If the user left the screen while that chain was running (easy on a slow phone): - dispose() saw "not submitted" and saved the draft back; - the !mounted early return skipped the clear entirely. Fix: - Retire the draft immediately after the create call succeeds, before any mounted check: set _submitted, cancel debounce timers, clear local and server drafts using singletons (not widget-scoped references, which may already be gone). - dispose() saves only when not submitted and not currently submitting; an in-flight submit owns the draft's fate. - If the submit fails after unmount, persist explicitly from values snapshotted before the await, because the text controllers are disposed by then. - The same ordering bug existed for reply and response drafts on the detail screen.

Hole 2: prefilled composers sharing the draft slot

"Resend" and "Fork" open the composer pre-filled with an existing post and skip loading the draft, but used the same storage slot. Backing out of an untouched resend saved the already-sent post as the draft (overwriting a real one), and submitting a resend cleared an unrelated draft. Fix: - Snapshot the prefill; _isUntouchedPrefill() short-circuits every draft writer. - _ownsLocalDraft is false for a prefilled composer until it actually writes; only an owner may clear the slot on submit. - An edited resend is unsent work and is still saved (and replaces the slot); accepted.

Hole 3: submit unmounts before follow-up writes

When the composer is unmounted mid-submit, follow-up calls (post metadata) never run. The client then has to find the new post by listing posts, which is fragile and expensive (an 800 KB list). Create endpoints should return the new id.

Debugging technique: reconstruct the screen flow from the access log

Local drafts are invisible to the server, but the app's requests are not. In the nginx access log (filter by the app's user agent, e.g. Dart/3.11 (dart:io)), opening the composer has a signature (GET /upload_status + /ai/projects + /ai/models). A composer-open signature with no POST /posts after it means "opened and backed out", which pinpointed the resend path. See Debugging client-side bugs from the server's access log.

Testing

Widget tests can pump the composer under ProviderScope with SharedPreferences.setMockInitialValues; the regression tests for hole 2 fail on the old code. Debounced editors (100 ms onChanged) need the debounce flushed or canceled in tests.

See also

Linked from

Sources: tldr memory (bug-draft-restored-after-post, bug-draft-prefill-saved), posts #1618, #1716