resource_repository 0.0.2 resource_repository: ^0.0.2 copied to clipboard
Implementation of a resource/repository pattern for data management in Flutter apps.
Resource Repository #
Implementation of a resource/repository pattern for data management in Flutter apps.
Getting started #
- Add dependency
flutter pub add resource_repository
- Package uses
MemoryCacheStorage
by default. You can add others:- Hive resource_repository_hive_storage. Recommended for small data.
- ObjectBox resource_repository_objectbox_storage. More efficient on big datasets.
Usage #
The repository can be "remote" or "local". Local should be used to manage data locally,
for example instead of shared_preferences when you need more than just a small key/value store.
The remote should provide a FetchData
callback to load data from an external place, e.g. REST API call.
final localRepository = StreamRepository.local({
CacheStorage<K, V>? storage, // pass any of the implementations of CacheStorage
});
final remoteRepository = StreamRepository.remote({
required FetchData<K, V> fetch,
CacheStorage<K, V>? storage, // SimpleMemoryCacheStorage will be used if null
Duration? cacheDuration, // Pass duration value or duration resolver for complex logic.
CacheDurationResolver<K, V>? cacheDurationResolver,
});
And then you can subscribe to resource, watch all resources or use other functionality of repository.
final repository = StreamRepository<String, FooBar>.remote(
fetch: (key, arguments) => apiCall(fooBarId: key),
cacheDuration: const Duration(minutes: 30),
storage: SimpleMemoryCacheStorage('foobar_storage_key'),
);
repository.stream('1234').listen((fooBar) {
// work with resource
});
// Do something
repository.invalidate('1234'); // reload
Additional information #
If you've created a new implementation of CacheStorage, please let me know on the project's github page. I'll add it to the readme.