offline_courier library
Offline-first outbox for Flutter: persists failed HTTP requests and replays them with exponential backoff + jitter when the network returns, even if the app was killed. Zero dependencies beyond the Flutter SDK.
Classes
- BackgroundScheduler
- Schedules OS-level jobs that drain the queue while the app is in the background or terminated.
- ConnectivityMonitor
- Abstraction over the platform network state so the courier can be driven by fakes in tests or by a custom reachability check.
- CourierConfig
-
Immutable configuration of an
OfflineCourier. - CourierDelivered
- The request reached the server immediately and was accepted (2xx).
- CourierError
- A background operation of the courier failed (native scheduler refused a job, storage error during an automatic sync, connectivity channel error…).
- CourierFailed
-
The request could not be sent for a reason that retrying cannot fix and
that produced no HTTP response (for example a
headersResolverthat threw a non-network error). It was moved to the dead-letter queue. - CourierNotification
-
Content of the persistent notification shown while a
CourierServiceruns. Android requires one for every foreground service. - CourierQueued
- The request could not be delivered now (offline, timeout, 5xx...) and was persisted. It will be retried automatically.
- CourierRejected
- The server answered with a permanent error (e.g. 422) while online. The request is not queued: retrying it unchanged would fail again, so the app should surface the error to the user right away.
- CourierRequest
- Immutable, serializable description of an HTTP request that can be sent now or persisted and replayed later.
- CourierResponse
- Minimal, immutable HTTP response returned by the courier.
- CourierResult
-
Outcome of
OfflineCourier.send. Sealed soswitchis exhaustive: - CourierService
-
An Android foreground service that keeps working with the screen off,
after the app is swiped away from Recents and, optionally, after a
reboot: every
intervalit runs your callback in its own Flutter engine, whereOfflineCourier.instanceis ready to queue requests. - CourierStore
- Persistence contract for the outbox.
- EventBridge
- Carries SyncEvents between engines of the same app (UI isolate and background job), so listeners see deliveries made by either of them.
- InMemoryCourierStore
- Volatile CourierStore for tests and prototypes. Nothing survives a restart, so never use it in production.
- IoRequestExecutor
-
Default RequestExecutor built on
dart:io's HttpClient: no external dependencies. - NativeBackgroundScheduler
- Default BackgroundScheduler, implemented natively by the plugin:
- NativeConnectivityMonitor
- Default ConnectivityMonitor implemented natively by the plugin:
- NativeCourierStore
-
Default, durable CourierStore: SQLite owned by the plugin's native side
(
android.database.sqliteon Android, systemlibsqlite3on iOS). No Dart packages involved. - NativeEventBridge
- EventBridge backed by the plugin: native code fans each published event out to every other engine attached in the process.
- OfflineCourier
- Entry point of the package: an offline-first outbox for HTTP requests.
- PayloadCipher
- Encrypts the serialized request (URL, headers and body) before it touches the disk. Queued requests often carry auth tokens and personal data, so production apps should plug in a real implementation (e.g. AES-GCM, with the key kept in the Android Keystore / iOS Keychain).
- PlainPayloadCipher
- No-op cipher (default). The payload is stored as plain JSON bytes.
- RequestExecutor
- Sends a CourierRequest over the wire. Swap it to add certificate pinning, a proxy or to fake the network in tests.
- RetryPolicy
- Immutable description of when and how many times a queued request is retried.
- SyncEngine
- Platform-agnostic core of the courier: owns the outbox lifecycle and the retry/backoff state machine.
- SyncEvent
- Everything observable that happens inside the courier.
- SyncEventCodec
- Serializes SyncEvents to plain maps so they can cross from one engine (e.g. the background job) to another (the UI isolate) through the platform channel.
- SyncProgress
-
Progress of the current drain cycle. Emitted at the start (
completed == 0) and after every processed task. - SyncReport
- Summary of one drain cycle.
- SyncTask
- A CourierRequest persisted in the outbox together with its delivery bookkeeping. Instances are immutable; use copyWith to derive new states.
- SyncTaskRowCodec
- Converts SyncTasks to the flat rows stored by the native SQLite layer.
- TaskDeadLettered
- A task failed permanently (4xx, retries exhausted or TTL expired) and was moved to the dead-letter queue. Use it to resolve conflicts in the UI.
- TaskDelivered
- A queued task was accepted by the server and removed from the outbox.
- TaskEnqueued
- A request was persisted in the outbox.
- TaskRetryScheduled
- An attempt failed transiently; the task will be retried after delay.
Enums
- CourierServiceState
-
Lifecycle of the
CourierService. - DeliveryOutcome
- What the engine should do with a task after an HTTP attempt.
- JitterStrategy
- How random noise is applied to an exponential backoff delay.
- OrderingMode
- Delivery order guarantees of the outbox.
- SyncTaskStatus
- Lifecycle state of a persisted SyncTask.
Functions
-
defaultCourierConfig(
) → CourierConfig -
Default configuration builder. A top-level function annotated with
@pragma('vm:entry-point')so the background engine can resolve it from its callback handle (without the pragma the AOT compiler tree-shakes it and the JIT refuses to closurize it from native code). -
defaultResponseClassifier(
int statusCode) → DeliveryOutcome - Default classification:
-
runBackgroundSync(
CourierConfig config, {Future< void> onEngineCreated(String owner)?}) → Future<Duration?> -
Drains the outbox once with
configand returns the delay until the next pending task is due, ornullif the queue is empty.
Typedefs
- AttemptResult = ({Object? error, CourierResponse? response})
- Result of a single network attempt: exactly one field is non-null.
-
ComponentBuilder<
T> = T Function() - Builds a component lazily, so the same config can be recreated inside the background engine.
- CourierErrorHandler = void Function(Object error, StackTrace stackTrace, String operation)
-
Receives failures that are not thrown to the caller (see
CourierError). -
CourierServiceCallback
= FutureOr<
void> Function() -
Work done on every tick of the CourierService: a top-level or static
function annotated with
@pragma('vm:entry-point'). -
HeadersResolver
= FutureOr<
Map< Function(CourierRequest request)String, String> > - Resolves headers at delivery time, e.g. a fresh auth token. Runs in the background engine as well, so it must not depend on UI state.
- ResponseClassifier = DeliveryOutcome Function(int statusCode)
- Decides what to do with a task given the HTTP status the server returned.