How To handle Initialization depending on two future providers #1722
Replies: 5 comments 1 reply
-
This situation, which I found in issues, is what I am trying to accomplish.
|
Beta Was this translation helpful? Give feedback.
-
In Flutter most database initialization, store prefs, etc are using asynchronous methods therefore this seems like a normal use case. What I can't find is how to go about it. |
Beta Was this translation helpful? Give feedback.
-
If you find yourself here I finally answered my own question via these blogs:
|
Beta Was this translation helpful? Give feedback.
-
@penhorwood Some example code would be helpful here. |
Beta Was this translation helpful? Give feedback.
-
I did not try it, but maybe you could use utils like this (based on flutter...\lib\async\future_extensions.dart). import 'package:hooks_riverpod/hooks_riverpod.dart';
extension AsyncValueRecord2<T1, T2> on (AsyncValue<T1>, AsyncValue<T2>) {
AsyncValue<(T1, T2)> get watch {
if ($1.hasError) {
return AsyncError($1.error!, $1.stackTrace!);
}
if ($2.hasError) {
return AsyncError($2.error!, $2.stackTrace!);
}
if ($1.isLoading || $2.isLoading) {
return const AsyncLoading();
}
return AsyncData(($1.requireValue, $2.requireValue));
}
}
extension AsyncValueRecord3<T1, T2, T3> on (
AsyncValue<T1>,
AsyncValue<T2>,
AsyncValue<T3>
) {
AsyncValue<(T1, T2, T3)> get watch {
if ($1.hasError) {
return AsyncError($1.error!, $1.stackTrace!);
}
if ($2.hasError) {
return AsyncError($2.error!, $2.stackTrace!);
}
if ($3.hasError) {
return AsyncError($3.error!, $3.stackTrace!);
}
if ($1.isLoading || $2.isLoading || $3.isLoading) {
return const AsyncLoading();
}
return AsyncData(($1.requireValue, $2.requireValue, $3.requireValue));
}
} and you can use it like so: Widget build(BuildContext context, WidgetRef ref) {
AsyncValue<First> firstAsync = ref.watch(firstProvider);
AsyncValue<Second> secondAsync = ref.watch(secondProvider);
AsyncValue<(First, Second)> combined = (firstAsync, secondAsync).watch;
...
} |
Beta Was this translation helpful? Give feedback.
-
I am super new to RiverPod so maybe I just missed how to do this. In my app I am using Firebase Cloud Messaging, Isar for the database, Sentry and some encryption with keys stored in Secure Prefs. All this stuff needs to be initialized. I was using a singleton initialization class to handle this which does work. What is the suggested way with RiverPod? The initialization has a definite order as some parts depend on others.
Beta Was this translation helpful? Give feedback.
All reactions