Sometimes I think I'm getting the logic of providers and then I get stumped for hours trying to do something like below.
I need to get a List of connection id's from a firestore collectionsstream. Easy.
However, I need to feed this streaming list of connection id's into another firestore collectionstream. Below, you can see my upcomingEventsStreamProvider is ref.watch both the database and the connectionsStream. There are no errors thrown by riverpod or firestore. However, in the logs, I see my print statements in this order:
returned data
returned null stream value
How am I abusing the power of Riverpod providers? Lol.
final connectionsStreamProvider = StreamProvider<List<UidConnections>>((ref) {
final database = ref.watch(databaseProvider);
return database != null ? database.connectionsStream() : Stream.value(null);
});
final connectionsListStateProvider = StateProvider<List>((ref) => []);
final upcomingEventsStreamProvider = StreamProvider<List<SpecialEvents>>((ref) {
final database = ref.watch(databaseProvider);
final connectionsStream = ref.watch(connectionsStreamProvider);
if (database != null && connectionsStream != null) {
connectionsStream.whenData((data) {
if (data != null) {
data.forEach((event) {
if (event?.active == true && event?.connectedUid != null) {
ref
.read(connectionsListStateProvider)
.state
.add(event.connectedUid);
}
});
print('returned data');
return database.upcomingSpecialEventsStream(
ref.read(connectionsListStateProvider).state);
}
});
}
print('returned null stream value');
return Stream.value(null);
});
Or maybe, do I just need to refactor my Firebase Cloud Firestore query to first obtain the connection id stream? I'd rather use Riverpod because I'll still need a stream of connection id's alone.