Search code examples
flutterdartriverpodflutter-isar

Watching a list triggers FutureProvider endlessly


I'm using Riverpod alongside Isar databases. I've wrapped an Isar query (Future<List<String>>) with a FutureProvider so I may watch this query for changes. However, my widgets keep on building over and over again from what I believe is the provider calling the notify again and again since lists are objects and don't have the same equality.

How can I watch this list without having my provider notify listeners endlessly? I'm pretty sure I might also be misunderstanding what is happening here.

Here is my code:

Provider:

@riverpod
Future<List<String>> spools(SpoolsRef ref) async {
  final databaseManager = await ref.watch(databaseManagerProvider.future); // Future<DatabaseManager>
  return databaseManager.spools; // Future<List<String>>
}

Consumer:

/// Get all spools and make [DropdownMenuEntries] from them.
List<DropdownMenuEntry> spoolsMenuEntries() {
  return ref
          .watch(spoolsProvider) // I THINK this line notifies endlessly
          .valueOrNull
          ?.map((spool) => DropdownMenuEntry(value: spool, label: spool))
          .toList() ??
      [
        const DropdownMenuEntry(
          value: defaultSpoolName,
          label: defaultSpoolName,
        ),
      ];
}

Solution

  • I was able to resolve this. It turns out that my Build function calls spoolsMenuEntries which in turn triggers a Build. This is what caused the loop.