jdixon04
jdixon04

Reputation: 1595

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

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!

Upvotes: 1

Views: 1833

Answers (1)

Omar Mahmoud
Omar Mahmoud

Reputation: 3087

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);

Upvotes: 1

Related Questions