flutter_offline_sync_push
Flutter adapters for flutter_offline_sync_core: Workmanager background sync, connectivity-triggered foreground sync, and startup recovery.
flutter_offline_sync_core stays unit-testable without Flutter widgets. This package owns everything that needs flutter, workmanager, or connectivity_plus.
Docs: Foreground & background · GitHub
Install
dependencies:
flutter_offline_sync_push: ^0.1.2Quick start
import 'package:dio/dio.dart';
import 'package:flutter_offline_sync_push/flutter_offline_sync_push.dart';
const _syncTaskName = 'com.example.app.syncTask';
// 1. Register handlers and create the engine (foreground).
final registry = SyncHandlerRegistry()
..register('uploadImage', UploadImageHandler(dio));
final db = SyncDatabase();
final engine = SyncEngine(db: db, registry: registry);
// 2. Foreground: recover + sync when connectivity returns.
final coordinator = FlutterSyncCoordinator(engine: engine);
await coordinator.startForegroundSync();
// 3. Background: top-level entrypoint required by Workmanager.
@pragma('vm:entry-point')
void callbackDispatcher() {
Workmanager().executeTask((task, inputData) {
return runBackgroundSync(createBackgroundSyncEngine);
});
}
@pragma('vm:entry-point')
Future<SyncEngine> createBackgroundSyncEngine() async {
final db = SyncDatabase(); // same sqlite path as foreground
final dio = Dio(BaseOptions(baseUrl: 'https://api.example.com'));
return SyncEngine(db: db, registry: buildRegistry(dio));
}
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await initBackgroundSync(
callbackDispatcher: callbackDispatcher,
uniqueName: _syncTaskName,
);
// ... rest of app startup
}API
| Export | Purpose |
|---|---|
runBackgroundSync | Single background sync pass inside a Workmanager task |
initBackgroundSync | Initialize Workmanager + register periodic task |
cancelBackgroundSync | Cancel a periodic task by name |
ConnectivitySyncTrigger | Call engine.syncAll() when connectivity returns |
FlutterSyncCoordinator | Startup recovery + connectivity trigger in one call |
This package re-exports flutter_offline_sync_core, so a single import covers both the engine and the Flutter glue.
Background isolate constraints
The Workmanager callback runs in a separate OS-spawned isolate with no shared memory. You must:
- Call
WidgetsFlutterBinding.ensureInitialized()(handled byrunBackgroundSync) - Re-open
SyncDatabaseat the same file path - Re-register every handler in a new
SyncHandlerRegistry
createEngine passed to runBackgroundSync must be a top-level or static function — closures cannot cross isolate boundaries.
Periodic background sync limits
- Android: minimum interval is 15 minutes
- iOS:
BGTaskScheduleris best-effort with no guaranteed interval
Use ConnectivitySyncTrigger / FlutterSyncCoordinator for low-latency sync while the app is in the foreground.
Example
cd packages/flutter_offline_sync_push/example
flutter runSee example/lib/background_sync.dart for Workmanager wiring and example/lib/main.dart for FlutterSyncCoordinator usage.
