Offline is not a toggle. It is a statement about what your app believes is true when nobody is watching. Most teams discover this the hard way: they ship a feature, then get a bug report that says “it works on wifi, it spins forever on the bus”.
The network is a cache, not a dependency
If a screen cannot render without a successful request, the network is a dependency. If it can render from local data and refresh in the background, the network is a cache. The difference shows up in three places.
- First paint: can the user do anything before the first byte arrives?
- Failure mode: does a dropped connection look like an error or like “slightly stale”?
- Conflict resolution: when two writes disagree, who wins?
Design the second and third before you write the first screen. Retrofitting them is a rewrite.
Model the queue, not the request
abstract class SyncState {
const SyncState();
}
class Synced extends SyncState {
const Synced(this.at);
final DateTime at;
}
class Pending extends SyncState {
const Pending(this.operation);
final Operation operation;
}
class Failed extends SyncState {
const Failed(this.operation, this.reason);
final Operation operation;
final String reason;
}
Every mutation becomes an operation in a queue. The queue is the source of truth for “what the user asked for”; the server is the source of truth for “what actually happened”. Reconcile them on reconnect.
Three rules I keep
- Read from local storage, always. The network only updates local storage.
- Writes append to a queue; the queue drains when online. Never block the UI on a write.
- Show staleness honestly. “Last updated 2 hours ago” is a feature, not an apology.
What changes in review
- No
await api.get()in a widget’s build path. - Every write has a retry policy and a visible pending state.
- Conflicts resolve to a user-visible choice, not a silent overwrite.
The bus is a real place. Design for it.