I have a FutureProvider with a List of items. How can I update an item in this list without a refresh the provider?
Asked
Active
Viewed 2,545 times
2 Answers
2
You should use a StateNotifierProvider for your list like this:
final listProvider = StateNotifierProvider<ListItem, dynamic>((ref) {
return ListItem([]);
});
class ListItem extends StateNotifier<dynamic> {
ListItem(List<dynamic> items) : super(items);
void update(dynamic item) {
// Your update logic. You have access to the use with 'state' variable
}
}
Before that you populate the listProvider
with your future response. Also you should change the dynamic
type to your object type on the list

Jonatan Orozco
- 103
- 5
-
1Yeah but I wanted to use futureProvider to be able to use ref.watch(myFutureProvider).when(data: data, error: error, loading: loading) and update a the state without refreshing and fectching the data again. – Nidhal Apr 13 '22 at 21:24
-
What about using another FutureProvider receiving your update? You await your original provider, but fetch your updated state. – Jonatan Orozco Apr 15 '22 at 22:18
-
no he should not as that was not the question. He might use `ref.refresh()` somewhere. This answer is only helpful to a whole different scenario – MwBakker May 10 '23 at 10:46
2
You can get the data you need from the creation function of the FutureProvider
. This will be executed on every update of the otherProvider
. So all you need to do is update this otherProvider
value and your future will refresh automatically.
late final FutureProvider<List<String>> allStrings = FutureProvider((ref) {
var aNewValue = ref.watch(otherProvider);
return _externalService.getMyStringsFromSomewhere();
});

James
- 2,516
- 2
- 19
- 31