flutter_sync_client 0.1.1
flutter_sync_client: ^0.1.1 copied to clipboard
Real-time data synchronization plugin for Flutter with Hive and Socket.IO
Flutter Sync Client #
Real-time data synchronization plugin for Flutter with Hive and Socket.IO.
Installation #
Add to your pubspec.yaml:
dependencies:
flutter_sync_client:
path: ../flutter_sync_client
Usage #
1. Initialize Client #
final syncClient = SyncClient(
serverUrl: 'http://your-server.com:3000',
);
await syncClient.initialize();
2. Get Collection Reference #
final todosCollection = syncClient.collection('todos');
3. Listen to Changes #
StreamBuilder<List<Map<String, dynamic>>>(
stream: todosCollection.stream,
builder: (context, snapshot) {
final todos = snapshot.data ?? [];
return ListView.builder(
itemCount: todos.length,
itemBuilder: (context, index) {
return ListTile(title: Text(todos[index]['title']));
},
);
},
)
4. CRUD Operations #
// Add
await todosCollection.add({
'title': 'Buy milk',
'completed': false,
});
// Update
await todosCollection.update(id, {
'completed': true,
});
// Delete
await todosCollection.delete(id);
// Get single item
final todo = await todosCollection.get(id);
// Get all items
final todos = await todosCollection.getAll();
5. Connection Status #
StreamBuilder<bool>(
stream: syncClient.connectionStream,
builder: (context, snapshot) {
final isConnected = snapshot.data ?? false;
return Icon(
isConnected ? Icons.cloud_done : Icons.cloud_off,
);
},
)
Features #
- ✅ Real-time synchronization
- ✅ Offline support with Hive
- ✅ Automatic reconnection
- ✅ Conflict resolution
- ✅ Change listeners (Streams)
- ✅ Energy efficient
- ✅ Simple API
Example #
See the example directory for a complete Todo app.
Testing #
cd example
flutter run
Try:
- Add todos
- Toggle completion
- Delete todos
- Close app and reopen (data persists)
- Stop server and make changes (offline mode)
- Restart server (auto sync)
License #
MIT