Riverpod FutureProvider 不断触发

Vis*_*ngh 4 dart flutter flutter-provider riverpod

我正在与家人一起使用 Riverpod 的 FutureProvider。FutureProvider 不断地运行。它仅显示加载对话框。热重载也停止工作。FutureProvider 在没有家人的情况下工作得很好。请帮忙找出问题所在。

在此输入图像描述

final ephemerisProvider =
    Provider((ref) => ApiService("https://localhost"));

final ephemerisFutureProvider = FutureProvider.family
    .autoDispose<EpheModel, Map<String, dynamic>>((ref, data) async {
  var response = await ref.read(ephemerisProvider).getData(data);
  print(EpheModel.fromJSON(response));
  return EpheModel.fromJSON(response);
});

class Kundlis extends ConsumerWidget {
  static const routeName = "/kundlis";
  @override
  Widget build(BuildContext context, ScopedReader watch) {
    final AsyncValue<EpheModel> kundlis = watch(ephemerisFutureProvider({}));
    return Scaffold(
        appBar: AppBar(
          title: Text("Kundlis"),
        ),
        drawer: AppDrawer(),
        body: kundlis.when(
            data: (kundli) => Center(child: Text(kundli.toString())),
            loading: () => ProgressDialog(message: "Fetching Details..."),
            error: (message, st) =>
                CustomSnackBar.buildErrorSnackbar(context, '$message')));
  }
}

class ApiService {
  final String url;
  ApiService(this.url);
  Future<Map<String, dynamic>> getData(Map<String, dynamic> data) async {
    try {
      http.Response response = await http.post(url + "/ephe",
          headers: <String, String>{'Content-Type': 'application/json'},
          body: jsonEncode(data));
      if (response.statusCode == 200) {
        return data;
      } else {
        throw Exception("Error Fetching Details");
      }
    } on SocketException {
      throw Exception("No Internet Connection");
    } on HttpException {
      throw Exception("Error Fetching Details");
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

Ran*_*rtz 12

{} != {}。因为.family,您每次调用时都在创建一个全新的提供者watch(ephemerisFutureProvider({}))。要通过系列选择先前构建的提供者,您必须传递相同的值。并且保证 {} 永远不会与 {} 相同。:)