Determine if a provider has a value #3356
-
Hi – my app has a provider that contains a list of bookings. I also have a booking notifier that allows operations on a single booking. When initialising the booking notifier, I want to use the value from the list of bookings if it's in there, whilst loading the data for the booking asynchronously. This allows the UI to show something more quickly and then to update if the data was stale. class BookingNotifier extends AutoDisposeFamilyAsyncNotifier<BookingInfo, int> {
@override
Future<BookingInfo> build(int arg) async {
// See if we can find the booking in the list of current bookings
final bookings = await ref.watch(Pods.currentBookings.future);
final bi = bookings.firstWhereOrNull((bi) => bi.booking.id == arg);
if (bi == null) {
// We'll need to wait for the data to load
return _loadBooking(arg);
} else {
// We can use the data we've got in the booking object whilst we wait
// for the async data load to complete
_loadBooking(arg)
.then((bookingInfo) => state = AsyncData(bookingInfo))
.withoutAwait();
return bi;
}
}
...
} This works fine, however, it will force provider Is there a way to check whether a provider is already loaded without triggering a load? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Are you looking for |
Beta Was this translation helpful? Give feedback.
Are you looking for
ref.exist(provider)
?