Refreshing FutureProvider.family or Multiple Providers
FlutterRiverpodI recently wondered how you could easily refresh a FutureProvider
.
Had I read the docs, I would have known of ref.refresh
.
Nonetheless, the solution I came up with is a bit more general and can be applied even for FutureProvider.family
or multiple FutureProvider
s.
Creating a refreshProvider
The main idea is to create a refreshProvider
that other providers can watch.
When the refreshProvider
refreshes, all other providers will be refreshed too.
One way of creating such a refreshProvider
is to use a ChangeNotifierProvider
:
import 'package:flutter/foundation.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
class RefreshNotifier extends ChangeNotifier {
void refresh() {
notifyListeners();
}
}
final refreshProvider = ChangeNotifierProvider<RefreshNotifier>(
(ref) => RefreshNotifier(),
);
Other providers can watch it, for example:
final itemProvider = FutureProvider.family<List<Item>, int>(
(ref, page) async {
ref.watch(refreshProvider);
final items = await fetchItems(page);
return items;
},
);
And here's the code needed to trigger a refresh:
ref.read(refreshProvider.notifier).refresh();
That's it! But keep in mind: For simpler usecases ref.refresh
is the better choice.