-
How to implement functionality when one provider needs to wait for data from another? @Riverpod(keepAlive: true)
class ResultsBestData extends _$ResultsBestData {
late String _userId;
late Group _group;
late User _player;
// late final stream;
@override
Future<ResultsBest> build() async {
_userId = ref.watch(loggedUserProvider.select((it) => it.id));
state = AsyncValue.loading(); //Loading indicator
_group = await ref.watch(groupSelectedProvider.future); //HERE
_player = await ref.watch(playerSelectedProvider.future);
return _fetchAndSetBestResults();
}
Future<ResultsBest> _fetchAndSetBestResults() async {
state = AsyncValue.loading();
try {
final url = Uri.parse(Globals.urlDB + '/user/${_player.id}/group/${_group.groupId}/result/best');
final response = await http.get(
... I have tried the suggested watch pair (bottom code), but in this case, I need to return null (or fake object) or an exception. This approach refreshes many times before it gets value. This is an undesired behaviour. @override
Future<ResultsBest> build() {
_userId = ref.watch(loggedUserProvider.select((it) => it.id));
final selectedGroup = ref.watch(groupSelectedProvider);
final selectedPlayer = ref.watch(playerSelectedProvider);
AsyncValue<(Group, User)> _combined = (selectedGroup, selectedPlayer).watch;
_combined.whenData((pairData) {
_group = pairData.$1;
_player = pairData.$2;
return _fetchAndSetBestResults();
});
//return null. or throw exception in build function //HERE
... In my case (top code) do I need to use some dependencies? If so, how? @Riverpod(keepAlive: true, dependencies: [....]) //HERE
class ResultsBestData extends _$ResultsBestData {... Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Using |
Beta Was this translation helpful? Give feedback.
Using
.future
is the correct approach here! :)state = AsyncValue.loading();
is not required as AsyncValue is already set to loading during the first build.