📶 Dio Offline Queue
A robust, 100% customizable, and production-ready Dio interceptor that automatically catches failed network requests (POST, PUT, DELETE, PATCH) when the device is offline, saves them, and replays them in FIFO order once connectivity is restored.
🚀 Features
- Automatic Queuing: Catches
DioExceptionrelated to connectivity and saves requests. - FIFO Replay: Ensures data consistency by replaying requests in the order they were made.
- Fully Abstracted: Swap out storage, network observation, or retry logic with your own implementations.
- Sensible Defaults: Comes with
InMemoryQueueStorageandConnectivityPlusObserverout-of-the-box. - Safe by Default: Only queues non-GET requests by default to prevent side-effect issues.
📦 Installation
Add the following to your pubspec.yaml:
dependencies:
dio_offline_queue: ^1.0.0
🛠️ Quick Start
import 'package:dio/dio.dart';
import 'package:dio_offline_queue/dio_offline_queue.dart';
void main() {
final dio = Dio();
// 1. Setup the manager with default implementations
final manager = OfflineQueueManager(
dio: dio,
storage: InMemoryQueueStorage(),
networkObserver: ConnectivityPlusObserver(),
retryPolicy: DefaultRetryPolicy(),
);
// 2. Add the interceptor to your Dio instance
dio.interceptors.add(OfflineQueueInterceptor(manager: manager));
// 3. That's it! Requests made while offline will now be queued and replayed.
}
⚙️ Advanced Customization
💾 Persistent Storage (Hive/SQLite)
The default InMemoryQueueStorage will lose data if the app is closed. For production, implement the QueueStorage interface using your preferred database:
class MyHiveStorage implements QueueStorage {
@override
Future<void> add(QueuedRequest request) async {
final box = await Hive.openBox('queue');
await box.put(request.id, request.toJson());
}
// Implement other methods...
}
🔄 Custom Retry Policy
Control exactly when and how requests should be retried:
final retryPolicy = DefaultRetryPolicy(
maxAttempts: 10,
retryDelay: Duration(seconds: 10),
);
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
Libraries
- dio_offline_queue
- A robust, customizable Dio interceptor that queues failed requests during offline periods and replays them automatically when connectivity is restored.