sync_offline_requests 1.2.1 copy "sync_offline_requests: ^1.2.1" to clipboard
sync_offline_requests: ^1.2.1 copied to clipboard

A Flutter package that provides offline-first HTTP request handling with automatic synchronization when internet connectivity is restored.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:sync_offline_requests/sync_offline_requests.dart';

void main() {
  // Set optional callbacks before initializing
  OfflineSync.onSyncStart = () {
    debugPrint('🔄 CALLBACK: Sync started');
  };

  OfflineSync.onRequestSuccess = (id) {
    debugPrint('✅ CALLBACK: Request synced → $id');
  };

  OfflineSync.onRequestFailure = (id, retry) {
    debugPrint('❌ CALLBACK: Request failed (retry $retry) → $id');
  };

  OfflineSync.onRequestsDiscarded = (count) {
    debugPrint('🗑️ CALLBACK: Discarded $count failed requests');
  };

  // Initialize with custom retry limit (default: 3)
  OfflineSync.initialize(maxRetryCount: 5);

  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'sync_offline_requests Demo',
      home: const HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int _pending = 0;

  Future<void> _refreshCount() async {
    final count = await OfflineSync.pendingCount();
    setState(() {
      _pending = count;
    });
  }

  @override
  void initState() {
    super.initState();
    _refreshCount();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Offline Sync Demo')),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              'Pending requests in SQLite: $_pending',
              style: const TextStyle(fontSize: 18),
            ),

            const SizedBox(height: 30),

            // POST request with Authorization header
            ElevatedButton(
              onPressed: () async {
                await OfflineSync.post(
                  url: 'https://jsonplaceholder.typicode.com/posts',
                  body: {
                    'title': 'Offline POST',
                    'body': 'Stored in SQLite',
                    'userId': 1,
                  },
                  headers: {'Authorization': 'Bearer demo_token'},
                );
                await _refreshCount();
              },
              child: const Text('POST Request'),
            ),

            // PUT request
            ElevatedButton(
              onPressed: () async {
                await OfflineSync.put(
                  url: 'https://jsonplaceholder.typicode.com/posts/1',
                  body: {
                    'title': 'Updated Title',
                    'body': 'Updated body',
                    'userId': 1,
                  },
                  headers: {'Authorization': 'Bearer demo_token'},
                );
                await _refreshCount();
              },
              child: const Text('PUT Request'),
            ),

            // DELETE request
            ElevatedButton(
              onPressed: () async {
                await OfflineSync.delete(
                  url: 'https://jsonplaceholder.typicode.com/posts/1',
                  headers: {'Authorization': 'Bearer demo_token'},
                );
                await _refreshCount();
              },
              child: const Text('DELETE Request'),
            ),

            const Divider(height: 40),

            ElevatedButton(
              onPressed: () async {
                await OfflineSync.syncNow();
                await _refreshCount();
              },
              child: const Text('Manual Sync'),
            ),

            ElevatedButton(
              onPressed: () async {
                await OfflineSync.clearAll();
                await _refreshCount();
              },
              child: const Text('Clear All Requests'),
            ),

            ElevatedButton(
              onPressed: () async {
                final removed = await OfflineSync.clearFailedOnly();
                debugPrint('Removed $removed failed requests');
                await _refreshCount();
              },
              child: const Text('Clear Failed Requests'),
            ),
          ],
        ),
      ),
    );
  }
}
7
likes
150
points
87
downloads

Publisher

unverified uploader

Weekly Downloads

A Flutter package that provides offline-first HTTP request handling with automatic synchronization when internet connectivity is restored.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

connectivity_plus, flutter, http, path, sqflite, uuid

More

Packages that depend on sync_offline_requests