sync_vault library
SyncVault - An offline-first data synchronization layer for Flutter.
SyncVault automatically queues API requests when the device is offline and syncs them with exponential backoff when connectivity is restored.
Quick Start
import 'package:sync_vault/sync_vault.dart';
// Initialize
await SyncVault.init(
config: SyncVaultConfig(baseUrl: 'https://api.example.com'),
);
// Make requests
final response = await SyncVault.instance.post(
'/users',
data: {'name': 'John'},
idempotencyKey: 'create_john_123',
);
if (response.isQueued) {
print('Request queued for later sync');
}
// Listen to sync status
SyncVault.instance.statusStream.listen((status) {
switch (status) {
case SyncStatus.syncing:
print('Syncing...');
case SyncStatus.synced:
print('All caught up!');
case SyncStatus.pending:
print('Has pending requests');
case SyncStatus.error:
print('Sync error occurred');
}
});
Event Bus (Track Individual Jobs)
// Listen to all events
SyncVault.instance.onEvent.listen((event) {
if (event.isSuccess) {
print('Action ${event.id} completed!');
}
});
// Or use SyncVaultListener widget
SyncVaultListener(
actionId: 'job_123',
onSuccess: (event) => showSnackBar('Job uploaded!'),
child: MyButton(),
)
Classes
- ActionFailed
- Action failed permanently (dead letter).
- ActionResult
- Result of processing a single sync action.
- ActionRetry
- Action failed but can be retried later.
- ActionSuccess
- Action was successfully sent to the server.
- HiveStorageAdapter
- Hive-based implementation of StorageAdapter.
- NetworkManager
- Manages network connectivity state and provides streams for monitoring.
- SqfliteStorageAdapter
- SQLite-based implementation of StorageAdapter.
- StorageAdapter
- Abstract interface for persisting SyncAction items.
- SyncAction
- Represents a single queued action to be synchronized.
- SyncEvent
- Represents a sync event for a specific action.
- SyncEventBuilder
- A widget that rebuilds when specific action events occur.
- SyncResponse
- Response from a SyncVault request.
- SyncStatusBuilder
- A widget that rebuilds when sync status changes.
- SyncVault
- Main entry point for SyncVault - an offline-first sync layer.
- SyncVaultConfig
- Configuration for SyncVault initialization.
- SyncVaultListener
- A widget that listens to SyncVault events and calls callbacks.
- Worker
- The queue processing engine.
Enums
- HttpMethod
- Represents the HTTP method for a sync action.
- SyncEventStatus
- Status of a sync event.
- SyncStatus
- The current synchronization status.
Exceptions / Errors
- DuplicateActionException
- Thrown when attempting to queue a duplicate action.
- MaxRetriesExceededException
- Thrown when a sync action exceeds maximum retry attempts.
- NetworkException
- Thrown when a network request fails.
- NotInitializedException
- Thrown when SyncVault is used before initialization.
- StorageException
- Thrown when storage operations fail.
- SyncVaultException
- Base class for all SyncVault exceptions.