【发布时间】:2022-10-19 21:58:36
【问题描述】:
我有一个带有项目列表的 FutureProvider。如何在不刷新提供程序的情况下更新此列表中的项目?
【问题讨论】:
我有一个带有项目列表的 FutureProvider。如何在不刷新提供程序的情况下更新此列表中的项目?
【问题讨论】:
您应该为您的列表使用 StateNotifierProvider,如下所示:
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
}
}
在此之前,您使用您未来的回复填充listProvider。此外,您应该将dynamic 类型更改为列表中的对象类型
【讨论】:
您可以从FutureProvider的创建功能中获取您需要的数据。这将在otherProvider 的每次更新时执行。所以你需要做的就是更新这个otherProvider 值,你的未来会自动刷新。
late final FutureProvider<List<String>> allStrings = FutureProvider((ref) {
var aNewValue = ref.watch(otherProvider);
return _externalService.getMyStringsFromSomewhere();
});
【讨论】: