simple_offline_sync 0.0.1 copy "simple_offline_sync: ^0.0.1" to clipboard
simple_offline_sync: ^0.0.1 copied to clipboard

A Flutter package for offline syncing.

example/lib/main.dart

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

void main() {
  runApp(const MyApp());
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Offline Sync Demo',
      theme: ThemeData(primarySwatch: Colors.blue),
      home: const OfflineSyncPage(),
    );
  }
}

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

  @override
  OfflineSyncPageState createState() => OfflineSyncPageState();
}

class OfflineSyncPageState extends State<OfflineSyncPage> {
  final OfflineSync _offlineSync = OfflineSync();
  final TextEditingController _textController = TextEditingController();
  final String _syncKey = 'offline_data';
  String _retrievedData = 'No data synced yet';

  Future<void> _saveData() async {
    final data = _textController.text;

    // Save data locally
    await _offlineSync.saveDataLocally(_syncKey, data);

    // Try to sync data
    bool success = await _offlineSync.syncDataToServer(
      _syncKey,
      (data) async {
        // Simulate API call
        await Future.delayed(const Duration(seconds: 2));
        debugPrint('Data synced to server: $data');
        return true; // Return true if sync is successful
      },
    );

    if (success) {
      debugPrint("Data synced successfully!");
    } else {
      debugPrint('Failed to sync data. Stored locally.');
    }
  }

  Future<void> _retrieveData() async {
    final data = await _offlineSync.getDataLocally(_syncKey);
    setState(() {
      _retrievedData = data ?? 'No data found';
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Offline Sync Example'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          children: [
            TextField(
              controller: _textController,
              decoration: const InputDecoration(
                labelText: 'Enter data to sync',
                border: OutlineInputBorder(),
              ),
            ),
            const SizedBox(height: 16),
            ElevatedButton(
              onPressed: _saveData,
              child: const Text('Save and Sync Data'),
            ),
            const SizedBox(height: 16),
            ElevatedButton(
              onPressed: _retrieveData,
              child: const Text('Retrieve Data'),
            ),
            const SizedBox(height: 16),
            Text(
              'Retrieved Data: $_retrievedData',
              style: const TextStyle(fontSize: 16),
            ),
          ],
        ),
      ),
    );
  }
}
3
likes
130
points
11
downloads

Publisher

unverified uploader

Weekly Downloads

A Flutter package for offline syncing.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

connectivity_plus, dio, flutter, riverpod, shared_preferences

More

Packages that depend on simple_offline_sync