riverpod_cache 0.0.5

SDKflutter
Platformandroidioswindowslinuxmacosweb

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.