dio_offline_sync 0.0.1
dio_offline_sync: ^0.0.1 copied to clipboard
A production-ready Flutter package for plug-and-play offline request queueing and auto-sync with Dio. Handles connectivity, storage, and retries automatically.
Dio Offline Sync #
A specific, production-ready Flutter package for plug-and-play offline request queueing & auto-sync with Dio.
🎯 What problem does this solve? #
Building robust offline support is often over-complicated. Developers usually have to:
- Manually check connectivity before every request.
- Wrap simple API calls in complex
try/catchlogic just for offline cases. - Manage local database, syncing queues, and retries manually.
Dio Offline Sync provides a "set it and forget it" solution. You write normal Dio code. We handle the rest.
📦 Installation #
Add the following to your pubspec.yaml:
dependencies:
dio_offline_sync: ^0.0.1
dio: ^5.0.0
hive: ^2.0.0
hive_flutter: ^1.0.0 # Recommended for storage initialization
🚀 usage #
1. Initialize Storage (in main.dart) #
import 'package:hive_flutter/hive_flutter.dart';
void main() async {
await Hive.initFlutter();
runApp(MyApp());
}
2. Add Offline Support (One-Line Setup) #
import 'package:dio/dio.dart';
import 'package:dio_offline_sync/offline_sync.dart';
final dio = Dio();
dio.addOfflineSupport(
storage: HiveOfflineStorage(),
syncStrategy: SyncStrategy.auto,
retryPolicy: RetryPolicy.exponential(
maxRetries: 3,
baseDelay: Duration(seconds: 2),
),
);
Now, just use Dio as usual:
// If offline, this returns a 202 Accepted and queues the request.
// When online, it syncs automatically.
await dio.post('https://api.example.com/data', data: {'id': 1});
🔄 Offline → Online Flow #
[ Device Offline ] [ Device Online ]
| |
v v
[ Dio Request ] [ Connectivity Restored ]
| |
(Connection Fail) v
| [ Trigger Sync ]
v |
[ Interceptor Catches ] v
| [ Read Queue (FIFO) ]
v |
[ Serialize Request ] v
| [ Replay Request ]
v |
[ Save to Local DB ] <---(Success)---------- [ Server Response ]
| |
v (Failure)
[ Return 202 ] |
v
[ Retry Policy ]
⚙️ Configuration Options #
| Parameter | Type | Default | Description |
|---|---|---|---|
storage |
OfflineStorage |
Required | The storage backend. HiveOfflineStorage is included. |
syncStrategy |
SyncStrategy |
auto |
auto (syncs when network returns) or manual. |
retryPolicy |
RetryPolicy |
Exponential |
Configures backoff and max retries for sync failures. |
⚠️ Limitations #
- Multipart/FormData: Requests containing
FormDataare currently ignored by the offline interceptor due to serialization complexities. - Server Errors: 4xx and 5xx errors are not queued. If the server is reachable but returns an error, the app receives it immediately.
- Response Handling: Offline requests return a generic
202 Acceptedresponse. Your UI code should handle this (e.g., "Saved pending sync"). - App Restart: Queued requests persist across restarts but syncing triggers on connectivity changes or manual calls.
📅 Future Roadmap #
- ❌ Support for
httppackage. - ❌ Support for
MultipartFileserialization (base64). - ❌ Custom offline response builders.
- ❌ Sync progress callbacks.