Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/masreplay/riverpod_cache
Add offline persistence support for Riverpod
https://github.com/masreplay/riverpod_cache
cahce dart flutter offline riverpod
Last synced: 4 days ago
JSON representation
Add offline persistence support for Riverpod
- Host: GitHub
- URL: https://github.com/masreplay/riverpod_cache
- Owner: masreplay
- License: mit
- Created: 2024-07-14T07:15:48.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2024-08-23T14:25:15.000Z (3 months ago)
- Last Synced: 2024-11-02T07:24:02.331Z (11 days ago)
- Topics: cahce, dart, flutter, offline, riverpod
- Language: C++
- Homepage: https://pub.dev/packages/riverpod_cache
- Size: 323 KB
- Stars: 5
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Riverpod offline persistence
[![Pub](https://img.shields.io/pub/v/riverpod_cache.svg)](https://pub.dev/packages/riverpod_cache)
[![GitHub stars](https://img.shields.io/github/stars/masreplay/riverpod_cache.svg?style=social)](https://github.com/masreplay/riverpod_cache)Add offline persistence support for Riverpod providers
## inspiration
Fix Riverpod issue of cache and offline persistence https://github.com/rrousselGit/riverpod/issues/1032## Features
- [x] Cache `FutureProvider`
- [x] Cache `StreamProvider`
- [x] Cache `StateNotifierProvider`
- [x] Cache `StateProvider`
- [x] And more...## Getting Started
In order to use this package, you need to add `riverpod_cache` as a dependency in your `pubspec.yaml` file.
```yaml
dependencies:
riverpod_cache: ^0.0.2
```Then, run `flutter pub get` to fetch the package.
## Usage
```dart
import 'package:riverpod_cache/riverpod_cache.dart';@riverpod
SharedPreferences sharedPreferences(SharedPreferencesRef ref) {
throw UnimplementedError();
}@riverpod
Stream todo(TodoRef ref) {
return ref.cacheFirstOfflinePersistence(
key: 'todo',
future: () async {
await Future.delayed(const Duration(seconds: 2));
final response = await Dio().get(
'https://jsonplaceholder.typicode.com/todos/1',
);final result = TodoResponse.fromJson(response.data);
return result;
},
sharedPreferences: ref.read(sharedPreferencesProvider),
fromJson: TodoResponse.fromJson,
toJson: (object) => object.toJson(),
);
}Future main() async {
WidgetsFlutterBinding.ensureInitialized();
final sharedPreferences = await SharedPreferences.getInstance();
runApp(
ProviderScope(
overrides: [
sharedPreferencesProvider.overrideWithValue(sharedPreferences),
],
child: const MainApp(),
),
);
}
```
## DocumentationFor more details, check out the [documentation](https://pub.dev/documentation/riverpod_cache/latest/).
## Contributing
Contributions are welcome! If you find any issues or have suggestions, please create a new issue or submit a pull request.
## License
This project is licensed under the [MIT License](./LICENSE).