local_first_hive_storage 0.1.0
local_first_hive_storage: ^0.1.0 copied to clipboard
Hive adapter for local_first: schema-less, offline-first storage with Hive boxes and metadata support.
local_first_hive_storage #
Child package of the local_first ecosystem. This Hive adapter provides schema-less, offline-first storage using Hive boxes, plus metadata support and reactive queries.
Why use this adapter? #
- Fast key/value storage with Hive.
- Schema-less: store your model maps directly, no column definitions needed.
- Namespaces for multi-user isolation (
useNamespace). - Reactive queries via
watchQuery. - Metadata storage via
setMeta/getMeta.
Installation #
Add the core and the Hive adapter to your pubspec.yaml:
dependencies:
local_first: ^0.5.0
local_first_hive_storage: ^0.1.0
Quick start #
import 'package:local_first/local_first.dart';
import 'package:local_first_hive_storage/local_first_hive_storage.dart';
class Todo with LocalFirstModel {
Todo({required this.id, required this.title})
: updatedAt = DateTime.now();
final String id;
final String title;
final DateTime updatedAt;
@override
Map<String, dynamic> toJson() => {
'id': id,
'title': title,
'updated_at': updatedAt.toIso8601String(),
};
factory Todo.fromJson(Map<String, dynamic> json) => Todo(
id: json['id'] as String,
title: json['title'] as String,
);
static Todo resolveConflict(Todo local, Todo remote) =>
local.updatedAt.isAfter(remote.updatedAt) ? local : remote;
}
final todoRepository = LocalFirstRepository<Todo>.create(
name: 'todo',
getId: (todo) => todo.id,
toJson: (todo) => todo.toJson(),
fromJson: Todo.fromJson,
onConflict: Todo.resolveConflict,
);
Future<void> main() async {
final client = LocalFirstClient(
repositories: [todoRepository],
localStorage: HiveLocalFirstStorage(),
syncStrategies: [
// Provide your own strategy implementing DataSyncStrategy.
],
);
await client.initialize();
await todoRepository.upsert(Todo(id: '1', title: 'Buy milk'));
}
Features #
- Schema-less Hive boxes (lazy boxes supported via
lazyCollections). - Namespaced storage with
useNamespace. - Reactive queries (
watchQuery) and standard CRUD operations. - Metadata table for app/client state (
setMeta/getMeta).
Testing #
Run tests from this package root:
flutter test
License #
MIT (see LICENSE).