riverpod_cache

Last updated:

0 purchases

riverpod_cache Image
riverpod_cache Images
Add to Cart

Description:

riverpod cache

Riverpod offline persistence #


Add offline persistence support for Riverpod providers
inspiration #
Fix Riverpod issue of cache and offline persistence https://github.com/rrousselGit/riverpod/issues/1032
Features #

✅ Cache FutureProvider
✅ Cache StreamProvider
✅ Cache StateNotifierProvider
✅ Cache StateProvider
✅ And more...

Getting Started #
In order to use this package, you need to add riverpod_cache as a dependency in your pubspec.yaml file.
dependencies:
riverpod_cache: ^0.0.2
copied to clipboard
Then, run flutter pub get to fetch the package.
Usage #
import 'package:riverpod_cache/riverpod_cache.dart';

@riverpod
SharedPreferences sharedPreferences(SharedPreferencesRef ref) {
throw UnimplementedError();
}

@riverpod
Stream<TodoResponse> 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<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
final sharedPreferences = await SharedPreferences.getInstance();
runApp(
ProviderScope(
overrides: [
sharedPreferencesProvider.overrideWithValue(sharedPreferences),
],
child: const MainApp(),
),
);
}
copied to clipboard
Documentation #
For more details, check out the documentation.
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:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.