Skip to content
BunBase BunBase BunBase Docs Alpha v0.1.0

Offline Queue — Flutter SDK

The offline queue captures write operations (create, update, delete) that fail due to no connectivity and replays them automatically when the device comes back online.

It uses connectivity_plus to detect connectivity changes and shared_preferences to persist the queue across app restarts.

Call startMonitoring() once at app startup:

void main() async {
WidgetsFlutterBinding.ensureInitialized();
await bunbase.auth.restoreSession();
await bunbase.offlineQueue.startMonitoring(); // restore + watch connectivity
runApp(MyApp());
}

Instead of calling collection.create() directly, enqueue it:

bunbase.offlineQueue.enqueue(
'POST',
'/api/v1/posts',
body: {'title': 'Written offline', 'body': 'Will sync later'},
);

Trigger a flush attempt at any time (e.g., after the user taps “Sync now”):

await bunbase.offlineQueue.flush();

React to each replayed item:

bunbase.offlineQueue.onFlushResult = (id, error) {
if (error == null) {
print('[$id] synced successfully');
} else {
print('[$id] failed: $error');
}
};
ResponseBehavior
2xxRemove from queue (success)
4xxRemove from queue (permanent failure) — onFlushResult called with error
5xxStop flush, keep item — retry on next connectivity event
Network errorStop flush, keep item — retry on next connectivity event
print('Pending: ${bunbase.offlineQueue.pendingCount}');
for (final item in bunbase.offlineQueue.items) {
print('${item.method} ${item.path} (enqueued: ${item.enqueuedAt})');
}

Discard all pending items without executing them:

bunbase.offlineQueue.clear();

Cancel the connectivity subscription (e.g., during testing or logout):

bunbase.offlineQueue.stopMonitoring();
FieldTypeDescription
idStringUnique item ID
methodStringHTTP method (POST, PATCH, DELETE)
pathStringAPI path, e.g. /api/v1/posts
bodyObject?JSON-serializable payload
enqueuedAtDateTimeWhen the item was enqueued

The queue is persisted in shared_preferences under the key bunbase_offline_queue. It survives app restarts and is restored automatically by startMonitoring().