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 build
ing 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,
),
];
}
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.