offline_sync_flutter 1.0.0 copy "offline_sync_flutter: ^1.0.0" to clipboard
offline_sync_flutter: ^1.0.0 copied to clipboard

An automatic offline-first API synchronization system for Flutter apps. Handles request queue management, automatic sync when internet returns, and conflict resolution.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:offline_sync_flutter/flutter_offline_sync.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  
  // Initialize the offline sync package
  await OfflineSync.initialize(
    retryAttempts: 3,
    retryDelay: const Duration(seconds: 5),
    conflictStrategy: ConflictStrategy.serverWins,
  );

  runApp(const MyApp());
}

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

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

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

  @override
  State<AttendanceScreen> createState() => _AttendanceScreenState();
}

class _AttendanceScreenState extends State<AttendanceScreen> {
  final _studentIdController = TextEditingController();
  bool _isPresent = true;

  Future<void> _submitAttendance() async {
    final studentId = _studentIdController.text;
    if (studentId.isEmpty) return;

    try {
      await OfflineSync.post(
        url: "https://jsonplaceholder.typicode.com/posts", // Dummy endpoint
        data: {
          "student_id": int.tryParse(studentId) ?? studentId,
          "present": _isPresent,
          "timestamp": DateTime.now().toIso8601String(),
        },
      );
      
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(content: Text('Attendance submitted!')),
      );
      
      _studentIdController.clear();
    } catch (e) {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('Error: $e')),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Attendance'),
        actions: const [
          PendingRequestsBadge(
            child: Padding(
              padding: EdgeInsets.all(8.0),
              child: SyncIndicator(),
            ),
          ),
          SizedBox(width: 16),
        ],
      ),
      body: Column(
        children: [
          const SyncStatusBanner(),
          Expanded(
            child: Padding(
              padding: const EdgeInsets.all(16.0),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  const Text(
                    'Try turning off your Wi-Fi/Data and submit attendance. The request will be queued. Turn it back on, and it will sync automatically!',
                    textAlign: TextAlign.center,
                  ),
                  const SizedBox(height: 32),
                  TextField(
                    controller: _studentIdController,
                    decoration: const InputDecoration(
                      labelText: 'Student ID',
                      border: OutlineInputBorder(),
                    ),
                    keyboardType: TextInputType.number,
                  ),
                  const SizedBox(height: 16),
                  SwitchListTile(
                    title: const Text('Present'),
                    value: _isPresent,
                    onChanged: (val) => setState(() => _isPresent = val),
                  ),
                  const SizedBox(height: 32),
                  ElevatedButton(
                    onPressed: _submitAttendance,
                    child: const Text('Submit Attendance'),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}
1
likes
0
points
109
downloads

Publisher

unverified uploader

Weekly Downloads

An automatic offline-first API synchronization system for Flutter apps. Handles request queue management, automatic sync when internet returns, and conflict resolution.

Repository (GitHub)
View/report issues

Topics

#offline #sync #api #network #offline-first

License

unknown (license)

Dependencies

connectivity_plus, flutter, hive, hive_flutter, http, path, path_provider, sqflite

More

Packages that depend on offline_sync_flutter