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.
Riverpod Community Extensions version 3 is only compatible with riverpod 3. If you want to use riverpod 2, revert to riverpod_community_extension v2.
This package adds the following methods to ref types:
-
cacheFor
onRef
- Prevents the provider from being disposed for the specified duration. -
cacheDataFor
onAsyncNotifier
- Keeps the data of the Async Notifier for the specified duration. -
debounce
onRef
- Wait for a specified duration before calling the provider's computation, and cancel the previous call if a new one is made. -
autoRefresh
onRef
- Refreshes the value at a specified interval. Useful for scenarios where periodic updates of a provider's value are required. -
refreshWhenReturningToForeground
onAsyncNotifier
- Refreshes the AsyncNotifier's value each time the app returns to the foreground, ensuring the data is always up to date after returning to the app. -
refreshWhenNetworkAvailable
onRef
- Automatically refresh the provider when the network is available if it has error state. Uses the package connectivity_plus. Will only for inside FutureProvider.
β In order to start using Riverpod Community Extensions you must have the [Dart SDK][dart_install_link] 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 = AsyncNotifierProvider<DataNotifier, int>(
DataNotifier.new,
isAutoDispose: true,
);
class DataNotifier extends AutoDisposeAsyncNotifier<int> {
@override
Future<int> build() async {
ref.cacheDataFor(const Duration(minutes: 5));
return await fetchDataFromApi();
}
}
Example with codegen:
import 'package:riverpod_community_extensions/riverpod_community_extensions.dart';
import 'package:riverpod/riverpod.dart';
part 'data_provider.g.dart';
@riverpod
class DataNotifier extends _$DataNotifier {
@override
Future<int> build() async {
ref.cacheDataFor(const Duration(minutes: 5));
return await fetchUserFromApi();
}
}
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.