Building an Offline-First Flutter App: Local Storage and Sync

How to make a Flutter app work without internet — choosing local storage, reading from a local database first, queueing changes and syncing when back online.

Darko Bačić, authorDarko Bačić 4 min read
On this page
  1. Decide how offline you need to be
  2. Choose local storage
  3. The pattern: the local database is the source of truth
  4. Writing while offline: an outbox
  5. Syncing
  6. Conflicts
  7. Show sync state in the UI

Users open apps in lifts, on trains, in basements and in places with one bar of signal. An app that shows a spinner forever — or loses what the user just typed — feels broken. Offline-first means the app reads and writes to local storage first and syncs with the server in the background. Here's how we approach it in Flutter.

##Decide how offline you need to be

Not every app needs full offline support. Pick the level that matches the product:

  1. 01Cache for reading. Show the last loaded data when offline. Enough for most content apps.
  2. 02Offline writes with a queue. Users can create and edit while offline; changes sync later. Good for notes, field work, checklists, logging.
  3. 03Full sync with conflict handling. Several devices or users edit the same data offline. Much harder — only build it if the product truly needs it.

Most MVPs need level 1 or 2.

##Choose local storage

  • shared_preferences — small key-value settings only. Not for app data.
  • sqflite / drift — SQLite. Drift adds type-safe queries, migrations and reactive streams; a strong default for structured, relational data.
  • Hive, Isar, ObjectBox — fast object stores; check the current maintenance status of the package before committing.
  • Firestore offline persistence — if you already use Firestore, its built-in cache gives you offline reads and queued writes with little code.

For most apps we pick drift: SQL is well understood, and reactive queries make the UI update automatically when local data changes.

##The pattern: the local database is the source of truth

The UI never talks to the network directly. It watches the local database, and a repository keeps that database in sync with the server.

dart
class NotesRepository {
  NotesRepository(this.db, this.api);
  final AppDatabase db;
  final NotesApi api;

  // The UI listens to this stream; it updates whenever local data changes.
  Stream<List<Note>> watchNotes() => db.watchAllNotes();

  // Refresh from the server when online; the stream updates automatically.
  Future<void> refresh() async {
    final remote = await api.fetchNotes();
    await db.upsertNotes(remote);
  }
}

The screen shows data instantly from disk, then refreshes quietly in the background. If the network fails, nothing breaks — the user just sees the last known data.

##Writing while offline: an outbox

When a user creates or edits something, save it locally immediately and record the change in an outbox table:

dart
Future<void> addNote(String text) async {
  final note = Note(id: uuid.v4(), text: text, updatedAt: DateTime.now());
  await db.transaction(() async {
    await db.insertNote(note);
    await db.enqueue(PendingChange.create(note));
  });
  sync(); // fire and forget; it retries later if offline
}

Two details matter:

  • Generate IDs on the device (UUIDs), so an item has a stable ID before the server ever sees it.
  • Write the item and the outbox entry in one transaction, so they can't get out of step.

##Syncing

A sync function sends pending changes in order, removes each one once the server confirms it, then pulls fresh data:

dart
Future<void> sync() async {
  for (final change in await db.pendingChanges()) {
    try {
      await api.push(change);
      await db.removePending(change.id);
    } catch (_) {
      return; // still offline or server error: try again later
    }
  }
  await refresh();
}

Trigger it when the app starts, when it returns to the foreground, after each local change, and when connectivity comes back (the connectivity_plus package can notify you — but treat it as a hint and let the request itself be the real test). For background sync while the app is closed, look at workmanager on Android and background fetch on iOS, and remember iOS decides when that runs.

##Conflicts

If the same item can be edited in two places, decide the rule up front:

  • Last write wins using a server timestamp — simple, fine for most personal data.
  • Field-level merge — combine changes to different fields.
  • Ask the user — only for data where losing an edit really matters.

Make server endpoints idempotent — sending the same change twice should have no extra effect — because retries will happen.

##Show sync state in the UI

Users trust offline apps when they can see what's happening: a small "Offline — changes will sync" banner, a pending icon on unsynced items, and a clear error if something couldn't be saved.

##Frequently asked questions

What is the best local database for Flutter?+

For structured data, drift (built on SQLite) is a strong default thanks to type-safe queries, migrations and reactive streams. For tiny settings, use shared_preferences.

Does Firebase work offline in Flutter?+

Yes. Firestore caches data and queues writes while offline on mobile, then syncs automatically when the connection returns.

How do I detect if a Flutter app is offline?+

The connectivity_plus package reports network changes, but having a network doesn't guarantee internet access. Treat it as a hint, and handle failed requests gracefully.

Is offline-first more expensive to build?+

Read caching adds little. Offline editing with sync adds noticeable work, and multi-device conflict handling adds more — so match the level of offline support to what users really need.