local_first_hive_storage 0.1.0 copy "local_first_hive_storage: ^0.1.0" to clipboard
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 #

Flutter Discord

Open Source Love pub package Full tests workflow codecov

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).

0
likes
150
points
124
downloads

Publisher

verified publishercarda.me

Weekly Downloads

Hive adapter for local_first: schema-less, offline-first storage with Hive boxes and metadata support.

Topics

#local-first #hive #storage

Documentation

API reference

License

MIT (license)

Dependencies

flutter, hive, hive_flutter, local_first, path

More

Packages that depend on local_first_hive_storage