riverpod_sync 0.0.1
riverpod_sync: ^0.0.1 copied to clipboard
A lightweight plugin that enables offline persistence for your Riverpod providers
Riverpod Sync #
A lightweight plugin that enables offline persistence for your Riverpod providers
Features #
- ✅ Cache
FutureProvider - ✅ Cache
StreamProvider - ✅ Cache
StateNotifierProvider - ✅ Cache
StateProvider - ✅ And more coming soon...
Getting Started #
In order to use this package, you need to add riverpod_sync as a dependency in your pubspec.yaml file.
dependencies:
riverpod_sync: ^0.0.1
Then, run flutter pub get to fetch the package.
Usage #
import 'package:riverpod_sync/riverpod_sync.dart';
@riverpod
SharedPreferences sharedPreferences(SharedPreferencesRef ref) {
throw UnimplementedError();
}
@riverpod
Stream<Response> fetch(Ref ref) {
return ref.cacheFirstOfflinePersistence(
key: 'todos',
future: () async {
final response = await Dio().get(
'https://jsonplaceholder.typicode.com/todos/1',
);
final result = TodoResponse.fromJson(response.data);
return result;
},
sharedPreferences: ref.read(sharedPreferencesProvider),
fromJson: Response.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(),
),
);
}
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.