crudrepo 0.3.4

SDKflutter
Platformandroidiosmacos

A package for managing CRUD operations with a repository pattern in Flutter.

crudrepo

A Dart package providing a flexible, generic repository pattern for CRUD (Create, Read, Update, Delete) operations with support for local SQLite and remote HTTP APIs, including optional caching.

Features

  • Generic CRUD Repositories: Easily create repositories for any model type.
  • Local Storage: Use SQLite via sqflite for persistent local storage.
  • Remote API Integration: Use dio for HTTP-based CRUD operations.
  • Caching Layer: Combine remote and local repositories for offline-first or cached data access.
  • Result Handling: All operations return Result types for robust error handling.
  • Custom Queries: Support for custom filters and raw SQL queries.

Getting Started

Add the dependency to your pubspec.yaml:

dependencies:
  crudrepo: ^0.2.0

Usage

Define Your Model

Your model should implement toJson() and have a constructor from Map<String, dynamic>:

class User {
  final int id;
  final String name;

  User({required this.id, required this.name});

  factory User.fromJson(Map<String, dynamic> json) =>
      User(id: json['id'], name: json['name']);

  Map<String, dynamic> toJson() => {'id': id, 'name': name};
}

Local Repository Example

import 'package:crudrepo/crudrepo.dart';

final localRepo = CrudRepositoryLocal<User>(
  datasource: FedsLocalSqflite(dbPath: 'assets/db/', dbName: 'app.db'),
  table: 'users',
  fromJson: User.fromJson,
);

// Create a user
final result = await localRepo.createItem(User(id: 1, name: 'Alice'));
result.fold(
  (user) => print('Created: ${user.name}'),
  (error) => print('Error: $error'),
);

Remote Repository Example

final remoteRepo = CrudRepositoryRemote<User>(
  datasource: DioService(),
  table: 'users',
  url: 'https://api.example.com',
  fromJson: User.fromJson,
);

Cached Repository Example

final cachedRepo = CrudRepositoryCached<User>(
  remoteDatasource: DioService(),
  localDatasource: FedsLocalSqflite(dbPath: 'assets/db/', dbName: 'app.db'),
  table: 'users',
  url: 'https://api.example.com',
  fromJson: User.fromJson,
);

API Reference

Testing

See test/crudrepo_test.dart for example tests.

Contributing

Contributions are welcome! Please open issues or submit pull requests.

License

MIT