offline_sync

Last updated:

0 purchases

offline_sync Image
offline_sync Images
Add to Cart

Description:

offline sync

OfflineSync #
OfflineSync is a Flutter package that provides offline-first data management and synchronization. It ensures smooth functionality even without an internet connection and syncs data once connectivity is restored.
Features #

Local data storage and retrieval
Automatic synchronization with server when online
Conflict resolution
Encryption of sensitive data
Batch syncing for improved performance
Error handling and retry mechanisms

Installation #
Add this to your package's pubspec.yaml file:
dependencies:
offline_sync: ^0.0.1
copied to clipboard
Usage #
Initialization #
First, initialize the OfflineSync instance in your app:
import 'package:offline_sync/offline_sync.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();
final offlineSync = OfflineSync();
await offlineSync.initialize();
runApp(MyApp());
}
copied to clipboard
Setting Custom API Endpoint #
Set a custom endpoint for your specific server:
final offlineSync = OfflineSync();
offlineSync.setApiEndpoint('https://your-custom-api.com');
copied to clipboard
Saving Data #
To save data locally and queue it for syncing:
final offlineSync = OfflineSync();
await offlineSync.saveLocalData('user_1', {
'name': 'John Doe',
'email': '[email protected]',
'age': 30,
});
copied to clipboard
Reading Data #
To read locally stored data:
final userData = await offlineSync.readLocalData('user_1');
if (userData != null) {
print('User name: ${userData['name']}');
} else {
print('User not found');
}
copied to clipboard
Syncing with Server #
The package automatically syncs data when an internet connection is available. However, you can manually trigger a sync:
try {
await offlineSync.updateFromServer();
print('Data updated from server successfully');
} catch (e) {
print('Failed to update from server: $e');
}
copied to clipboard
Handling Authentication #
Set the authentication token for API requests:
await offlineSync.setAuthToken('your_auth_token_here');
copied to clipboard
Advanced Usage: #
Conflict Resolution #
The package includes basic conflict resolution. You can customize this by extending the OfflineSync class:
class CustomOfflineSync extends OfflineSync {
@override
Future<Map<String, dynamic>> resolveConflict(
String id,
Map<String, dynamic> localData,
Map<String, dynamic> serverData
) async {
// Implement your custom conflict resolution strategy here
// This example prefers local changes
final resolvedData = Map<String, dynamic>.from(serverData);
localData.forEach((key, value) {
if (value != serverData[key]) {
resolvedData[key] = value;
}
});
return resolvedData;
}
}
copied to clipboard
Batch Processing #
The package processes sync queue in batches. You can adjust the batch size:
class CustomOfflineSync extends OfflineSync {
@override
int get batchSize => 100; // Default is 50
}
copied to clipboard
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.

License:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.