offline_sync_helper 1.0.1
offline_sync_helper: ^1.0.1 copied to clipboard
A fully customizable offline-first sync engine for Flutter apps with support for Hive, Drift, and custom storage adapters.
๐ก offline_sync_helper #
A fully customizable offline-first sync engine for Flutter apps.
offline_sync_helper simplifies the process of working with local data, tracking changes, and syncing them with a remote backend. It offers built-in support for Hive, Drift, Firebase, and is extensible to any local storage system or API format.
๐งฉ Why offline_sync_helper? #
Building apps that work offline and sync intelligently when online is a pain for most developers.
This package provides:
- โ Easy offline storage integration
- โ Automatic background sync when connectivity is restored
- โ Customizable conflict resolution strategies
- โ Full control over local/remote adapters and sync flows
๐ Features #
- ๐ Offline-first architecture
- ๐ Automatic sync on app launch, connectivity change, or manual trigger
- ๐ง Change tracking for inserts, updates, deletes
- ๐ ๏ธ Pluggable adapters for local and remote storage
- ๐งฉ Conflict resolution strategies: client wins, server wins, timestamp, or custom
- ๐ Encrypted local storage support (via Hive)
- ๐ถ Connectivity-aware retry mechanism
- ๐งช Testable architecture with mock support
- ๐ค Arabic & RTL support
๐ฆ Installation #
Add the package to your pubspec.yaml:
dependencies:
offline_sync_helper: ^1.0.0
connectivity_plus: ^5.0.2
hive: ^2.2.3
http: ^1.2.1
path_provider: ^2.1.2
You can replace Hive with Drift, Firebase, or other local storage plugins based on your adapter implementation.
๐ ๏ธ Getting Started #
1. Initialize the Sync Engine #
import 'package:offline_sync_helper/offline_sync_helper.dart';
await OfflineSyncHelper.initialize(
localAdapter: HiveAdapter<Task>(),
remoteSyncService: ApiSyncService(),
conflictResolver: TimestampResolver(),
);
2. Save Data Locally #
await OfflineSyncHelper.save(
model: Task(id: 1, name: 'Collect Feedback'),
);
The change is stored locally and synced automatically when the device goes online.
๐งฑ Architecture Overview #
+-------------------+
| LocalAdapter |<------- Hive / Drift / Firebase
+-------------------+
|
โผ
+-------------------+ +---------------------+
| SyncManager |<--------->| RemoteSyncService |
+-------------------+ +---------------------+
|
โผ
+-------------------+
| ConflictResolver |<--- Custom, Timestamp, Client, Server
+-------------------+
๐ Adapter Interfaces #
Local Adapter #
abstract class LocalAdapter<T> {
Future<void> saveLocally(T model);
Future<List<T>> getPendingChanges();
Future<void> markAsSynced(List<T> models);
}
Remote Sync Service #
abstract class RemoteSyncService<T> {
Future<SyncResult> sync(List<T> models);
}
โ๏ธ Conflict Resolution #
Built-in options:
ClientWinsResolverโ local data always overwrites remoteServerWinsResolverโ remote data always overwrites localTimestampResolverโ newer data (based on timestamp) wins
Create your own:
class CustomResolver implements ConflictResolver<Task> {
@override
Task resolve(Task local, Task remote) {
return local.priority > remote.priority ? local : remote;
}
}
๐ Sync Triggers #
- App launch
- Internet reconnect
- Manual
syncNow()method - Periodic background task (coming soon)
๐ Secure Local Storage #
Hive supports AES encryption. To enable:
var box = await Hive.openBox(
'tasks',
encryptionCipher: HiveAesCipher(my32ByteKey),
);
๐ Configuration Options #
OfflineSyncHelperConfig(
retryPolicy: RetryPolicy.maxAttempts(3),
batchSize: 20,
syncInterval: Duration(minutes: 5),
enableConnectivityWatcher: true,
autoSyncOnConnectivityChange: true,
);
๐ฑ Supported Platforms #
- โ Android
- โ iOS
๐ Internationalization #
Supports:
- RTL layouts
- Arabic and multi-language labels
intlpackage compatibility
๐ Example Use Cases #
| Use Case | Description |
|---|---|
| Field Agent App | Collect data in remote areas, sync later |
| Delivery Tracker | Update delivery statuses offline |
| E-Commerce Cart | Allow cart actions without internet |
| CRM / Lead Tracker | Sales staff update records offline |
๐ Full Example
For a complete working example with multiple field types, custom themes, and step-by-step forms, check out:
๐ Full Example on GitHub
License #
MIT
Author #
sdkwala.com