CloudSyncHiveAdapter
A Hive-powered implementation of the SyncAdapter
from the cloud_sync
package.
This adapter enables fast, offline-first local persistence of metadata and content using Hive. Great for syncing notes, logs, or custom data structures locally with cloud fallback support.
β¨ Features
- πΎ Stores metadata (
SyncMetadata
) and detailed content (String
) using Hive. - β‘ Fast and efficient with Hive's key-value storage.
- π Compatible with
cloud_sync
for hybrid local/cloud sync. - π΄ Fully offline-capable β ideal for mobile, embedded, or disconnected use cases.
π¦ Installation
Add dependencies to your pubspec.yaml
:
dependencies:
hive_ce: ^latest
cloud_sync: ^latest
cloud_sync_hive_adapter: ^latest
β Make sure you initialize Hive and register any needed adapters before use.
π Usage Example
import 'dart:convert';
import 'package:hive_ce/hive.dart';
import 'package:cloud_sync_hive_adapter/cloud_sync_hive_adapter.dart';
import 'your_models/my_metadata.dart'; // Define your SyncMetadata model
void main() async {
await Hive.initFlutter(); // or Hive.init('path')
// Optionally register your custom metadata adapter, if needed
// Hive.registerAdapter(MyMetadataAdapter());
final metadataBox = await Hive.openBox<String>('metadataBox');
final detailBox = await Hive.openLazyBox<String>('detailBox');
final adapter = CloudSyncHiveAdapter<MyMetadata>(
metadataBox: metadataBox,
detailBox: detailBox,
metadataToJson: (meta) => jsonEncode(meta.toJson()),
metadataFromJson: (json) => MyMetadata.fromJson(jsonDecode(json)),
getMetadataId: (meta) => meta.id,
isCurrentMetadataBeforeOther: (a, b) => a.updatedAt.isBefore(b.updatedAt),
);
// Use with CloudSync:
// final cloudSync = CloudSync(adapter: adapter);
}
π§± Class Overview
class CloudSyncHiveAdapter<M extends SyncMetadata>
extends SerializableSyncAdapter<M, String>
π§ Constructor Parameters
Parameter | Description |
---|---|
metadataBox |
Box<String> for serialized metadata (JSON ). |
detailBox |
LazyBox<String> for detailed string content. |
metadataToJson |
Converts metadata to JSON String . |
metadataFromJson |
Parses JSON String into metadata. |
getMetadataId |
Returns the unique ID for a metadata object. |
isCurrentMetadataBeforeOther |
Compares metadata objects for version ordering (e.g., by updatedAt ). |
β When to Use
- π² Need fast, reliable local sync for mobile or desktop.
- π΄ Want offline-first functionality for notes, logs, tasks, etc.
- π©οΈ Plan to combine Hive storage with cloud sync (e.g., Google Drive or Firebase).
- π§ͺ Prototyping or testing sync logic locally.
β οΈ Notes
- Store only
String
data in Hive boxes with this adapter. - For complex object storage, use Hiveβs custom type adapters directly or extend the adapter.
- Avoid storing large binary data β Hive is optimized for structured key-value data.
π Related Packages
cloud_sync
β Sync core and abstraction layer.hive_ce
β Lightweight, blazing-fast NoSQL database for Flutter/Dart.
π License
MIT (or your projectβs license).