riverpod_cache 0.0.4 copy "riverpod_cache: ^0.0.4" to clipboard
riverpod_cache: ^0.0.4 copied to clipboard

Add offline persistence support for Riverpod

Riverpod offline persistence #

Pub GitHub stars

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

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(),
    ),
  );
}

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.

5
likes
140
points
829
downloads

Publisher

verified publishermatheer.com

Weekly Downloads

Add offline persistence support for Riverpod

Repository (GitHub)
View/report issues

Topics

#riverpod #cache #offline #persistence #shared-preferences

Documentation

Documentation
API reference

Funding

Consider supporting this project:

github.com

License

MIT (license)

Dependencies

flutter, riverpod, riverpod_annotation, shared_preferences

More

Packages that depend on riverpod_cache