Replies: 3 comments 2 replies
-
In general, here is an example on how to implement a refresh timer. link I'm unsure if a familyprovider fits your needs, due to the issues that you've already discovered. // alt. implement as class in order to modify
@riverpod
Duration refreshDuration(..) => const Duration(seconds: 5);
@riverpod
Data dataProvider(..) async {
Timer? timer;
final data = await fetchData();
ref.listen(
refreshDurationProvider,
(previous, next) => timer = Timer(next, () => ref.invalidateSelf());,
fireImmediately: true,
);
ref.onDispose(timer?.cancel);
return data;
} |
Beta Was this translation helpful? Give feedback.
-
You're not really supposed to have a generic "http fetcher" provider. Consider making one provider per http request. Then those providers can define their own refresh timer. @riverpod
Future<Example> example(ExampleRef ref) {
final timer = Timer(duration, () => ref.invalidateSelf());
ref.onDispose(timer.cancel);
return http.get(...);
} |
Beta Was this translation helpful? Give feedback.
-
This is probably a bit hacky but you can do something like the following with Riverpod 3: @riverpod
T periodicData<T>(PeriodicDataRef ref, ProviderBase<T> prov, Duration d) {
final val = ref.watch(prov);
final timer = Timer(d, () => ref.invalidate(prov));
ref.onDispose(timer.cancel);
return val;
}
// Call it like this:
ref.watch(PeriodicDataProvider(DataProvider(args), Duration(seconds: 5)))
// Override the value
class MockData extends Data {
@override
build(args) =>// return the mock data here.
}
DataProvider(args).overrideWith(()=>MockData()); This solves the override issue by only overriding the 'data' part and not overriding the periodic fetcher. |
Beta Was this translation helpful? Give feedback.
-
I want to update widgets by polling a data source.
If possible I would like to use a syntax like:
final value = ref.watch(DataProvider(duration));
to indicate that a widget should refresh every "duration".
This has a couple of issues, namely:
I may be missing some already existing functionality, or this might be considered an anti-pattern so I'm open to any other solutions.
Thanks :)
Beta Was this translation helpful? Give feedback.
All reactions