Useful extensions on ref types for Riverpod.
Ref types are the way to interact with providers in Riverpod. They are built to be composable and flexible. This package provides some useful extensions on ref types to make them even more powerful and easily add common functionalities on your providers, such as auto-refreshing for example.
This package adds the following methods to ref types:
-
cacheFor
onAutoDisposeRef
- Prevents the provider from being disposed for the specified duration. -
cacheDataFor
onAutoDisposeFutureProviderRef
- Keeps the data of the future provider for the specified duration. -
debounce
onAutoDisposeFutureProviderRef
- Wait for a specified duration before calling the provider's computation, and cancel the previous call if a new one is made. -
autoRefresh
onAutoDisposeRef
- Refreshes the value at a specified interval. Useful for scenarios where periodic updates of a provider's value are required. -
refreshWhenReturningToForeground
onAutoDisposeRef
- Refreshes the provider's value each time the app returns to the foreground, ensuring the data is always up to date after returning to the app. -
refreshWhenNetworkAvailable
onAutoDisposeFutureProviderRef
- Automatically refresh the provider when the network is available if it has error state. Uses the package connectivity_plus.
β In order to start using Riverpod Community Extensions you must have the Dart SDK installed on your machine.
Install via dart pub add
:
dart pub add riverpod_community_extensions
Simply import the package and use the provided extensions on your ref types.
Example without codegen:
import 'package:riverpod_community_extensions/riverpod_community_extensions.dart';
import 'package:riverpod/riverpod.dart';
final dataProvider = FutureProvider.autoDispose((ref) async {
ref.cacheDataFor(const Duration(minutes: 5));
return fetchData();
});
Example with codegen:
import 'package:riverpod_community_extensions/riverpod_community_extensions.dart';
import 'package:riverpod/riverpod.dart';
part 'data_provider.g.dart';
@riverpod
Future<int> data((ref) async {
ref.cacheDataFor(const Duration(minutes: 5));
return fetchData();
});
We are a 130 people company developing and designing universal applications with React Native and Flutter using the Lean & Agile methodology. To get more information on the solutions that would suit your needs, feel free to get in touch by email or through or contact form!
We will always answer you with pleasure π
If you want to contribute to this project, please read the CONTRIBUTE.md file.