Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Riverpod FutureProvider keeps on firiging again and again

I am using Riverpod's FutureProvider with family. The FutureProvider keeps on running again and again. It shows the loading dialog only. Also the hot reload stops working. FutureProvider is working fine without family. Please help in finding what's wrong.

enter image description here

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");
    }
  }
}
like image 584
Vishal Singh Avatar asked Oct 29 '25 05:10

Vishal Singh


2 Answers

{} != {}. Because of .family, you are creating a completely new provider every time you call watch(ephemerisFutureProvider({})). To select a previously-built provider via family, you must pass an identical value. And {} is never identical to {}, guaranteed. :)

like image 195
Randal Schwartz Avatar answered Oct 30 '25 21:10

Randal Schwartz


I was able to resolve this by making the param provided to provider.family to extend Equatable . It worked fine after that

like image 24
Funyinoluwa Kashimawo Avatar answered Oct 30 '25 23:10

Funyinoluwa Kashimawo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!