Flutter Offline-First Starter Kit
Modular Flutter 3.47 & Dart 3.13 starter architecture with Drift SQLite local persistence, Riverpod state, durable outbox syncing, and conflict resolution.
01. Problem & Context
Mobile applications that treat remote REST APIs as the primary state store freeze on poor cellular connections, lose user edits on app kills, and struggle with race conditions when concurrent local mutations sync out of order.
02. Engineering Solution
Built `flutter-offline-first-starter`, a clean 4-layer Flutter 3.47 / Dart 3.13 architecture powered by Drift (SQLite) and Riverpod. Commits all user mutations atomically to local SQLite alongside a durable `sync_operations` outbox, updates the UI immediately via reactive streams, and drives opportunistic synchronization through a decoupled headless `SyncEngine` with single-flight locking, exponential backoff + jitter, crash recovery, and conflict resolution.
03. System Architecture
Enforces strict 4-layer clean separation: Presentation (widgets, banners, notifiers) → Application (`TaskService`, Riverpod providers, headless `SyncEngine`) → pure Dart Domain (`Task`, `SyncOperation`, `ConflictRecord`, sealed `Result<T>` with zero Flutter/Drift/HTTP imports) → Infrastructure (Drift SQLite DAOs, HTTPS-enforced `RemoteApiClient`, secret-redacting logger, Network Monitor).
Core Architectural Components
ACID Drift SQLite Persistence & Durable Outbox (`sync_operations`)
Writes entity changes (`tasks`) and queued `sync_operations` inside a single SQLite ACID transaction so reactive Drift streams update the UI instantaneously with zero data loss across crashes.
Headless `SyncEngine` with Single-Flight Locking & Coalescing
Coordinates background sync independently of widgets using single-flight locking, mutation coalescing, exponential backoff with jitter, orphan in-flight operation crash recovery, and tombstone purge lifecycles.
Pure Dart Domain Layer & Sealed `Result<T>` Control Flow
Keeps entities (`Task`, `SyncOperation`, `ConflictRecord`) and repository contracts free of Flutter, Riverpod, Drift, or HTTP dependencies while replacing untyped exceptions with exhaustive `Success<T>` / `Failure<T>` types.
Conflict Resolution, HTTPS Guard & Demo Simulation Controls
Detects revision conflicts into inspectable `ConflictRecord` objects, enforces HTTPS-only production remote endpoints with token/password log redaction, and includes an abstract HTTP client + interactive demo app simulating offline/500 failure modes.
04. Notable Engineering Decisions
Atomic Local Writes + Durable Outbox Over Optimistic-Only Network Calls
In-memory optimistic queues vanish if the OS terminates the app mid-flight. Writing both domain state and `sync_operations` in one SQLite transaction guarantees durability across restarts.
Zero-Framework Pure Dart Domain Layer with Sealed `Result<T>`
Keeping domain models and business rules independent of Flutter UI and Drift makes core invariants fast to unit test and trivial to reuse across mobile or desktop targets.
Single-Flight Locking, Coalescing & Crash Recovery
Rapid user toggles while offline can otherwise flood servers or strand operations in an `in_progress` state after a crash; coalescing and orphan recovery keep sync deterministic.
05. Engineering Challenges & Solutions
Challenge: Preventing duplicate concurrent sync loops when connectivity flickers or multiple UI actions fire.
Engineering Solution: Implemented single-flight locking inside `SyncEngine` paired with mutation coalescing and exponential backoff with jitter.
Challenge: Handling deletions offline without deleted items reappearing on the next pull sync.
Engineering Solution: Added a structured tombstone deletion and purge lifecycle alongside revision-based `ConflictRecord` tracking.
06. Implementation Highlights
- Clean 4-layer Flutter 3.47 / Dart 3.13 architecture with pure Dart Domain (`Result<T>` sealed `Success`/`Failure`) and Drift SQLite DAOs
- Atomic ACID local mutations paired with a durable SQLite `sync_operations` outbox and reactive UI streams
- Headless `SyncEngine` featuring single-flight locking, mutation coalescing, exponential backoff with jitter, and crash orphan recovery
- Tombstone deletion/purge lifecycle and structured `ConflictRecord` conflict detection & resolution
- HTTPS-enforced production `RemoteApiClient`, secret-redacting logger, and interactive demo app with failure simulation controls
- Verified with 16 automated test files (76 passing Flutter unit/widget tests, 0 `flutter analyze` issues) in GitHub Actions CI