riverpod provider called once - why are subsequent calls to the provider "cached?"

517

the provider will not be disposed while the widget in the tree you need to remove it from the back stack to be disposed

in your case, you need to reload it on other screens by calling

  context.refresh(pendingUploadsCounterProvider);
Share:
517
jdixon04
Author by

jdixon04

Animal Lover, Founder at Scout 🐶 http://scoutforpets.com #dogs Mobile + Web Developer 🔥 http://flutterigniter.com #flutter

Updated on December 07, 2022

Comments

  • jdixon04
    jdixon04 over 1 year

    I'm using Riverpod and I have a relatively simple provider that I'm using to get the number of pending writes from Firestore. This is basically used to help give users feedback as to whether or not their data is fully synced.

    final pendingUploadsCounterProvider =
        FutureProvider.autoDispose.family<int, List<Report>>((ref, reports) async {
      final db = ref.read(firebaseServiceProvider).db;
      final reportIds = reports.map((e) => e.id).toList();
    
      final documents = await Future.wait([
        for (final name in kReportTypes)
          db
              .collection(name)
              .where('report_id', whereIn: reportIds)
              .get(GetOptions(source: Source.cache))
      ]);
    
      return documents.where((event) => event.metadata.hasPendingWrites).length;
    });
    

    As you can see, all I'm doing is reading some data from the Firestore cache and then filtering that list based on the hasPendingWrites property.

    I'm attempting to use this provider on multiple screens as I need to provide the user feedback on their sync status in multiple locations.

    When I use this provider on a single screen only, every time I enter the screen, the provider is triggered and I get an up to date status.

    When I use this provider on multiple screens, the first time I enter a screen where it is used, the provider is triggered. Subsequently, entering any other screen where this provider is used, the provider is no longer triggered and therefore, the results are out of date.

    I thought by using autoDispose, the provider would be disposed of when I leave the screen, but that doesn't appear to be the case.

    I'm sure that there's a disconnect with my understanding of riverpod and how this works and hopeful someone can shed some light on my misunderstanding.

    Thank you!

  • jdixon04
    jdixon04 over 2 years
    Thanks, Omar! Is there a more elegant way to handle what I'm trying to accomplish?
  • Omar Mahmoud
    Omar Mahmoud over 2 years
    I think you can use it as StreamProvider to keep data up to date