Reuseable Firestore and Riverpod handlers #547
stargazing-dino
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi ! I've been messing around with providers and Firestore and I think there's room for improvement when it comes to dealing with these low level streams and futures. I'm thinking of making it a small one off package or gist but I'm sure there are others who can improve upon what I'm imagining.
Note I'm not asking for anything to be done on riverpod side. This is more suggestions to a question.
My idea is that we have a
CollectionHandler
class that handles one specific firestore collection. For example, say we have acars
collection.CollectionHandler does nothing but expose 4 fields:
For example, drilling down into
allSnapshotsStream
, we see its just a manager class that itself exposes variousproviders
concerning the documents in the collection.It's very common that someone wants just one doc as a stream. Or that they'll need it as provider, so they'll be able to reuse the same value somewhere else in the tree. I've also found the implementing this logic itself is a hassle when you need to do it for each collection.
With the above, it's as simple as getting the
carsHandlerProvider
and then getting whatever you need from there.Based off this, we can also make slight improvements. Say the user has already watched and fetched
carsHandler.docsProvider
and then they want to drill into one specific doc. A smart version ofAllSnapshotsStream.docProvider
can first check a cache or return a brand new snapshot.The fact that you can paginate streams yet it's such a hassle in practice seems to add to the idea of adding a ready made solution. With something like
paginatedSnapshotsStream
andriverpod
we get pagination but also get value reuse.One issue I've found with my proposal is the use of the
late
keyword which might be an anti-pattern in riverpod.Either way, I'm happy to hear any ideas around alternatives.
Here is a very rough sketch of my code although I want to stress it's not really about the code but more about a solution to have an efficient wrapper around very common
Firestore
collection functions for (non/)streamed documents/doc-changes.Beta Was this translation helpful? Give feedback.
All reactions