Replies: 1 comment 1 reply
-
I don't think riverpod provides a simple solution to your problem out of the box.
That's a good idea! Overall, try to keep it declarative. In general:Does your app really allow multiple instances of the CarbookingsProviderFamily to exist at the same time? Often the old instances is replaced (disposed) by the new one. Also
Updating and then invalidating makes the update unnecessary, as I understand it. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hey guys, thanks for trying to help me! 💯
Currently developing an app that has an entity called CarBooking which looks like this:
Therefore I have a CarBookingProvider (Family) which takes an DateTimeRange as an
DateTimeRange range
argument which fetches the CarBookings that are (partially or fully) contained inside the range:When creating a CarBooking I want to invalidate all CarBookingProviders that might be affected. What would be the riverpod way to do so?
Calling ref.invalidate on the complete family seems kind of overkill since it would invalidate providers which are not affected by the change and therefore cause unnecessary rebuild/fetch requests.
Solutions I considered
car_bookings_provider.dart
file like:and thus keeping track of currently watched ranges (via adding the range in the build and removing it via the ref.onDispose()).
Then in the
createBooking
function of the notifier I can check that list and decide which ranges should be affected by the new booking and call ref.invalidate(carBookingsProvider(range)) for that.Same approach as the private variable, just put it inside a provider to make it testable. Alternatively I could make the
_watchedRanges
variable public and mark it with@visibleForTesting
Creating an "Event" Stream in the repository that can be subscribed to and propagates all the Events (e.g. CRUD Operations). Then I can listen to that stream for each family provider member and the provider itself could decide wether to invalidate itself (or even just add it to its state)
Evaluation
The more I think about the problem, the more I tend to create an (private) StreamController that exposes a stream of events and each provider member listens to that. The reasons I tend to that solution:
Beta Was this translation helpful? Give feedback.
All reactions