dio_offline_queue 1.0.2 copy "dio_offline_queue: ^1.0.2" to clipboard
dio_offline_queue: ^1.0.2 copied to clipboard

A customizable Dio interceptor that queues failed requests during offline periods and replays them automatically when connectivity is restored.

example/example.dart

import 'package:dio/dio.dart';
import 'package:dio_offline_queue/dio_offline_queue.dart';

void main() async {
  final dio = Dio(BaseOptions(baseUrl: 'https://jsonplaceholder.typicode.com'));

  // 1. Initialize components (using defaults)
  final storage = InMemoryQueueStorage();
  final networkObserver = ConnectivityPlusObserver();
  final retryPolicy = DefaultRetryPolicy(
    maxAttempts: 3,
    retryDelay: const Duration(seconds: 5),
  );

  // 2. Initialize the Manager
  final manager = OfflineQueueManager(
    dio: dio,
    storage: storage,
    networkObserver: networkObserver,
    retryPolicy: retryPolicy,
  );

  // 3. Add the Interceptor
  dio.interceptors.add(
    OfflineQueueInterceptor(
      manager: manager,
      whitelistedMethods: [
        'POST',
        'PUT',
        'DELETE',
      ], // Optional: defaults to non-GET
    ),
  );

  // 4. Make a request while offline
  print('Making request...');
  try {
    final response = await dio.post(
      '/posts',
      data: {
        'title': 'Offline Post',
        'body': 'This was sent while offline!',
        'userId': 1,
      },
    );

    if (response.statusCode == 202) {
      print('✅ Request queued: ${response.statusMessage}');
    } else {
      print('✅ Request successful: ${response.data}');
    }
  } catch (e) {
    print('❌ Request failed: $e');
  }
}

/// Example of a custom Persistent Storage using a hypothetical Database
class MyPersistentStorage implements QueueStorage {
  @override
  Future<void> add(QueuedRequest request) async {
    // Save to Hive, SQLite, etc.
    // await db.insert('offline_queue', request.toJson());
  }

  @override
  Future<void> clear() async => {}; // db.delete('offline_queue')

  @override
  Future<List<QueuedRequest>> fetchAll() async => []; // db.query(...)

  @override
  Future<void> remove(String requestId) async => {}; // db.delete(...)

  @override
  Future<void> update(QueuedRequest request) async => {}; // db.update(...)
}
1
likes
160
points
161
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

A customizable Dio interceptor that queues failed requests during offline periods and replays them automatically when connectivity is restored.

Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

collection, connectivity_plus, dio, flutter, meta

More

Packages that depend on dio_offline_queue