local_first 0.2.0 copy "local_first: ^0.2.0" to clipboard
local_first: ^0.2.0 copied to clipboard

Local-first Flutter package for offline sync with support for Hive, SQLite, and common databases. Fast, lightweight, and reliable.

Local First #

Flutter Discord

Open Source Love pub package Full tests workflow codecov


Local-first data layer for Flutter apps that keeps your data available offline and synchronizes when the network returns. The goal is to provide a lightweight, pluggable engine that works with common local stores (Hive, SQLite, etc.) and can sync to any backend through custom strategies.

Status: early preview. The package is in active development and the public API may change before the first stable release. Use the examples below as guidance for the intended design.

Demo

Why local_first? #

  • Fast, responsive apps that read/write locally first and sync in the background.
  • Works without a network connection; queues changes and resolves conflicts when back online.
  • Storage-agnostic: start with our Hive, SQLite, or your preferred custom database implementation.
  • Backend-agnostic: create your sync strategies, so you can integrate with your existent REST, gRPC, WebSockets, etc.
  • Minimal boilerplate: declare the storage, register your models with repositories, and start syncing.

Features #

  • Offline-first caching with automatic retry when connectivity is restored.
  • Typed repositories for common CRUD flows.
  • Conflict handling strategies (last-write-wins, timestamps, and custom resolution hooks).
  • Background sync hooks for push/pull cycles.
  • Encryption-ready storage layer by leveraging your chosen database/provider.
  • Dev-friendly: simple configuration, verbose logging, and test utilities.

Installation #

Add the dependency to your pubspec.yaml:

dependencies:
  local_first: ^0.2.0

Then install it with:

flutter pub get

Quick start #

The API is evolving, but the intended flow looks like this:

import 'package:local_first/local_first.dart';

// 1) Describe your model.
class Todo with LocalFirstRepository<Todo> {
  Todo({
    required this.id, 
    required this.title, 
    this.completed = false,
  }) : updatedAt = DateTime.now();

  final String id;
  final String title;
  final bool completed;
  final DateTime updatedAt;

  @override
  Map<String, dynamic> toJson() => {
        'id': id,
        'title': title,
        'completed': completed,
        'updated_at': updatedAt.toIso8601String(),
      };

  @override
  static Todo fromJson(Map<String, dynamic> json) => Todo(
        id: json['id'] as String,
        title: json['title'] as String,
        completed: json['completed'] as bool? ?? false,
        updatedAt: DateTime.parse(json['updated_at']),
      );

  // Last write wins
  @override
  static Todo resolveConflict(
    Todo local,
    Todo remote,
  ) => local.updatedAt.isAfter(remote.updatedAt)
      ? local
      : remote;
}

Future<void> main() async {
  // 2) Wire up local + remote data sources (implementation details coming soon).
  final client = LocalFirstClient(
    repositories: [
      LocalFirstRepository.create(
        name: 'todo',
        getId: (todo) => todo.id,
        toJson: (todo) => todo.toJson(),
        fromJson: (json) => Todo.fromJson(json),
        onConflict: Todo.resolveConflict, 
      ),
    ],
    localStorage: HiveLocalFirstStorage(), // or SQLiteStorage/your own storage
    syncStrageties: [
      // Add your own custom sync strategies
      WebSocketSyncStrategy(
        baseUrl: 'wss://api.example.com',
        endpoint: '/sync',
      ),
      PushNotificationSyncStrategy(),
      WorkManagerSyncStrategy(
        minPeriod: Duration(minutes: 30),
      )
    ],
  );

  await client.initialize();

  // 3) Use the repository as if you were online the whole time.
  final repo = client.repository<Todo>();

  await repo.upsert(Todo(id: '1', title: 'Buy milk'));

  // served instantly from local cache
  final todoStream = await repo.query().orderBy('title').watch();

  // 4) Trigger manually when it makes sense (app start, pull-to-refresh, background).
  await client.sync();
}

The classes above illustrate the design goals; exact names and signatures may shift as the package matures.

Example app #

A starter Flutter app lives in example/ and showcases the local-first flow to increment or decrement a global shared counter (per-user namespaces, repositories, and a Mongo sync mock).

# Clone and fetch deps
git clone https://github.com/rafaelsetragni/local_first.git
cd local_first
flutter pub get

# Run the sample
cd example
flutter pub get
flutter run

Roadmap #

  • ❌ Implement Hive and SQLite storage adapters via add-on packages.
  • ❌ Provide REST and WebSocket sync strategies via add-on packages.
  • ❌ Background sync helpers for Android/iOS via add-on packages.
  • ✅ End-to-end sample app with authentication.
  • ❌ Comprehensive docs and testing utilities.

Contributing #

Contributions are welcome! Please open an issue to discuss ideas or bugs, and feel free to submit pull requests once we agree on the approach. Running the test suite before sending changes helps keep the package stable:

flutter test

License #

This project is available under the MIT License. See LICENSE for details.

1
likes
0
points
424
downloads

Publisher

verified publishercarda.me

Weekly Downloads

Local-first Flutter package for offline sync with support for Hive, SQLite, and common databases. Fast, lightweight, and reliable.

Repository (GitHub)
View/report issues

Topics

#local-first #repository #sync #offline-first

License

unknown (license)

Dependencies

flutter, hive_flutter

More

Packages that depend on local_first