Cleanest way to watch for several async providers without nesting AsyncValue.when #3468
-
Hello, I am wondering what is the most elegant way to deal with cases like this:
So to properly build this page I need to wait for 3 providers: // One provider to get the even "general" information and the ID's of admins
@riverpod
Future<Event> event(EventRef ref, int eventId) {
final eventRepo = ref.watch(eventRepoProvider);
return eventRepo.fetchEvent(id: eventId);
}
// One provider to get the list of members
@riverpod
Future<List<Member>> eventMembers(EventMembersRef ref, int eventId) {
final eventMembershipsRepo = ref.watch(eventRepoProvider);
return eventMembershipsRepo.getMembers(id: eventId);
}
// One provider to know if the current user is admin
@riverpod
Future<bool> isCurrentUserAdmin(IsCurrentUserAdminRef ref, int eventId) {
final userId = ref.watch(authRepoProvider).currentUser.uuid;
final event = await ref.watch(eventProvider(eventId));
return event.admins.contains(userId);
} Now in my UI how can I gracefully wait on these 3 providers to be ready (aka AsyncValue has a value) without nesting 3 IdeasUsing a new provider that returns all data @riverpod
Future<AComplexReturnObject> eventPage(EventPageRef ref, int eventId) {
final isAdmin = await ref.watch(isCurrentUserAdmin(eventId));
final members = await ref.watch(eventMembersProvider(eventId));
final event = await ref.watch(eventProvider(eventId));
return AComplexReturnObject(
idAdmin: isAdmin,
event: event,
members: members);
} Using an "is ready" provider@riverpod
Future<bool> eventPageisLoading(EventPageReadyForDisplay ref, int eventId) {
final isAdminProvider = ref.watch(isCurrentUserAdmin(eventId));
final membersProvider = ref.watch(eventMembersProvider(eventId));
final eventProvider= ref.watch(eventProvider(eventId));
return isAdminProvider.isLoading() || membersProvider.isLoading() || eventProvider.isLoading()
}
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Maybe a nice generic API can be created for that. A possible starting point could be the |
Beta Was this translation helpful? Give feedback.
-
A possible implementation here : #1722 (comment) |
Beta Was this translation helpful? Give feedback.
A possible implementation here : #1722 (comment)