r/FlutterDev • u/stephencolor • 40m ago
Article I built an offline-first sync queue in Flutter without sync packages. Here is what broke.
I wanted optimistic writes in a notes app: tap, see it immediately, let the network catch up. No sync package — just a queue, a tiny in-memory backend for development, and later a real API.
The queue is a list of intents, not API calls. Each operation stores: kind (create/update/delete), entity, entity id, payload, the base payload and base version it was written against, status, attempt count, next attempt time, and an idempotency key. Here is what it took to get right.
1. A timeout is not a failure.
My first version retried when a request timed out. Sometimes the server had already committed the write. Result: duplicate records, and in one case a duplicate invoice in a staging test.
The fix is a client-generated idempotency key per operation, sent as a header. The server stores the key with its result and replays that result when it sees the key again. Retry is now safe, including after a crash between "server committed" and "client saved the ack".
This is not optional or an edge case. A timeout after commit is normal behaviour on mobile networks.
2. Coalescing is only safe before the first attempt.
I merged queued edits for the same record to cut requests. Then I merged an edit into an operation that had already been sent once. Same key, new payload. The server either rejects it or, worse, stores something the user never wrote.
The queue now coalesces only when status == pending && attempts == 0. After any attempt, a new edit becomes a new operation with a new key. On create I merge edits into the create while it is still pending; once it is on the wire, the next edit is a separate update that waits for the create's server id.
3. Temp IDs live in more places than the row.
Offline creates get local_1. When the server answers with srv_42, the obvious step is to update the record's id. But other queued payloads still say local_1: parent ids, assignee ids, nested comment ids. The server rejects them or links the wrong thing.
The fix scans the queue and rewrites the id in entityId and recursively inside every payload map and list, then persists the mapping so old routes and old widgets still resolve. Order matters: rewrite before the next operation is sent, not after.
4. A failed head must block its own entity.
I tried skipping a failed operation and continuing with later writes for the same record. If an update fails and a delete succeeds, the server ends up in a state the user never saw.
Each entity is now a chain. Only the head is eligible. A failed or conflicted head blocks later writes for that entity and nothing else other entities keep syncing. The blocked head retries with jittered backoff, and when it needs a decision it waits for one.
What I stopped doing
- Global FIFO. One slow record stopped everything; per-entity ordering is enough and much faster.
- Retrying 400/422. A rejected payload will be rejected forever; it now goes straight to a dead-letter state.
- Retrying into a 401. The queue pauses, keeps attempts, and waits for
resumeWithAuth(). - Mutating an operation after it was sent.
- Showing a spinner for sync state. A chip ("3 waiting", "1 failed") plus a badge per row tells the user more in less space.
What I still find hard
Conflict UX. I have a field-by-field sheet now (merge / keep mine / keep theirs / discard), with server-wins, client-wins and field-merge policies for the automatic cases. But deciding which policy fits a screen is a product decision, not a technical one.
When an offline edit loses to a server change, how do you present it in Flutter without leaking the sync layer into every screen? I would genuinely like to see how other people structure that.
The queue, widgets and a demo app (with a fake backend, offline switch, latency, injected failures and a remote-edit button) are all part of one small package I put together after this experiment. Happy to answer questions about any of the four bugs; the code is small enough to read.