riverpod_repo 5.0.0
riverpod_repo: ^5.0.0 copied to clipboard
A Lib to build Riverpod providers based on the reposirory interfaces.
Repo Generator with Riverpod #
Getting Started #
This is a Flutter code generator to generate Riverpod Providers based on the Repository pattern.
The repository pattern is used to separate the concerns of data access and storage from the rest of the application logic. It provides a consistent interface for accessing and manipulating data regardless of the underlying data storage technology, such as a database or file system.
In this pattern, the application interacts with a repository interface, which acts as a mediator between the data access layer and the business logic layer. The repository interface provides a set of methods that can be used to perform operations on the data.
How To #
- Create a abstract class (Interface for your repository)
- Add @riverpodRepo annotation to your interface class
- Add the part directive for your own provider (
.g.dart) generated byriverpod_generator
part 'data_repo.g.dart';
- Create a provider to access the implementation of your repository
- Run the builder
dart run build_runner build --delete-conflicting-outputs
- It will create a single self-contained file
data_repo.repo.g.dartcontaining a Riverpod provider for every method defined in your repository. Import that file wherever you need the providers.
Note: Starting from version 5.0.0, the generator produces a single standalone
*.repo.g.dartlibrary built on the public Riverpod API. There is no longer an intermediate*.repo.dartpart file, and the repository providers are no longer processed byriverpod_generator.
Example #
// File data_repo.dart
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:riverpod_repo/annotations.dart';
part 'data_repo.g.dart';
@Riverpod(keepAlive: true)
RepoData repoData(Ref ref) => RepoDataImpl();
@riverpodRepo
abstract class RepoData {
Future<List<String>> getBooks({String search = '', String categoryId = ''});
Future<List<int>> getTopGenres();
Future<List<bool>> getTopBooksByGenre(String genreId, {String search = ''});
Future<List<String>> getCategories({String search = ''});
}
class RepoDataImpl implements RepoData {
@override
Future<List<String>> getBooks({String search = '', String categoryId = ''}) {
// The implementation logic should be written in this section
}
@override
Future<List<String>> getCategories({String search = ''}) {
// The implementation logic should be written in this section
}
@override
Future<List<bool>> getTopBooksByGenre(String genreId, {String search = ''}) {
// The implementation logic should be written in this section
}
@override
Future<List<int>> getTopGenres() {
// The implementation logic should be written in this section
}
}
This will generate the following providers in a single standalone file
data_repo.repo.g.dart
// GENERATED CODE - DO NOT MODIFY BY HAND
import 'package:riverpod/riverpod.dart';
import 'data_repo.dart';
export 'data_repo.dart';
/// Repository: RepoData, Method: getBooks
final repoDataGetBooksProvider = FutureProvider.autoDispose
.family<List<String>, ({String search, String categoryId})>((ref, arg) {
return ref
.watch(repoDataProvider)
.getBooks(search: arg.search, categoryId: arg.categoryId);
});
/// Repository: RepoData, Method: getTopGenres
final repoDataGetTopGenresProvider =
FutureProvider.autoDispose<List<int>>((ref) {
return ref.watch(repoDataProvider).getTopGenres();
});
/// Repository: RepoData, Method: getTopBooksByGenre
final repoDataGetTopBooksByGenreProvider = FutureProvider.autoDispose
.family<List<bool>, (String, {String search})>((ref, arg) {
return ref.watch(repoDataProvider).getTopBooksByGenre(arg.$1, search: arg.search);
});
/// Repository: RepoData, Method: getCategories
final repoDataGetCategoriesProvider = FutureProvider.autoDispose
.family<List<String>, ({String search})>((ref, arg) {
return ref.watch(repoDataProvider).getCategories(search: arg.search);
});
The provider type is chosen automatically from the method's return type:
Future<T> becomes a FutureProvider, Stream<T> becomes a StreamProvider,
and any other return type becomes a plain Provider.
Now you can use your Riverpod Providers anywhere in your application. Methods
without parameters expose a simple provider, while methods with parameters
expose a .family keyed by the method's arguments (a single positional
argument is passed directly, otherwise a Dart record is used):
// No parameters
ref.watch(repoDataGetTopGenresProvider);
// Single positional argument
ref.watch(repoDataGetCountryProvider('US'));
// Named / multiple arguments are passed as a record
ref.watch(repoDataGetCategoriesProvider((search: 'flutter'))).when(
loading: () => const CircularProgressIndicator(),
error: (err, stack) => Text('Error: $err'),
data: (categories) => Text(categories.toString()),
);
Important #
- The providers will be prefixed with the Repo Class name
@riverpodRepo
abstract class RepoData {
}
- Repo Class name and the provider you create for that class names should be same
@Riverpod(keepAlive: true)
RepoData repoData(Ref ref) => RepoDataImpl();
License (MIT) #
Copyright (c) 2021 Chatura Dilan Perera
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Support #
Please contact dilan@dilan.me for support and Sample Application