0

我有一个显示产品的简单页面,我正在尝试使用riverpod 和futureprovider,但是仅在我第一次访问该页面时才调用future?

final productInfo = FutureProvider<Map<String, dynamic>>((ref) async {
  final response =
      await http.get('https://www.api.com/$clickedID');
  final content = json.decode(response.body) as Map<String, dynamic>;
  return content;
});

4

3 回答 3

2

使用最近的 Riverpod,您可以ref.refresh(userProvider)刷新提供程序。

于 2021-09-21T15:14:28.280 回答
0

没有内置功能,但您可以在以后使用相同的未来覆盖该值。

recallProductInfoFutureProvider() {
final response = http.get('https://www.api.com/$clickedID');
  final content = json.decode(response.body) as Map<String, dynamic>;
productInfo.overrideWithValue(content)
}

考虑与 StreamProvider 一起使用并在 Consumer 内部观察,以在 overrideWithValue 上更新 UI。

于 2021-02-10T08:34:56.500 回答
0

请用

final userProvider = StreamProvider<User>(
 final response =
      await http.get('https://www.api.com/$clickedID');
  final content = json.decode(response.body) as Map<String, dynamic>;
  return content;
);

现在您可以使用watch,

  AsyncValue<User> user = watch(userProvider);
于 2020-12-12T17:19:06.160 回答