Replies: 3 comments 6 replies
-
AsyncTodosNotifier has access to "ref". It can use it to read other providers. |
Beta Was this translation helpful? Give feedback.
3 replies
-
I'm doing that now. @riverpod
Future<List<int>> intListFuture(IntListFutureRef ref) async {
await Future.delayed(const Duration(seconds: 1));
if (Random().nextInt(2).isOdd) {
throw Exception('int oops');
}
return List.generate(10, (index) => index);
}
@riverpod
Future<String> stringFuture(StringFutureRef ref) async {
await Future.delayed(const Duration(seconds: 2));
if (Random().nextInt(2).isOdd) {
throw Exception('string oops');
}
final data =
await ref.watch(intListFutureProvider.selectAsync((data) => data));
return (data[Random().nextInt(data.length)] * 20).toString();
}
class Child extends ConsumerWidget {
const Child({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
return ref.watch(stringFutureProvider).when(
data: (data) => TextButton(
onPressed: () {
ref.invalidate(intListFutureProvider);
},
child: Text(data)),
error: (e, s) => TextButton(
onPressed: () {
if (ref.read(intListFutureProvider).hasError) {
ref.invalidate(intListFutureProvider);
} else {
ref.invalidate(stringFutureProvider);
}
},
child: Text(e.toString())),
loading: () => const Text('loading'),
);
}
} |
Beta Was this translation helpful? Give feedback.
2 replies
-
@moxiaov587 @rrousselGit how about |
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
-
How can I combine 2
AsyncNotifierProvider
s where oneAsyncNotifierProvider
is dependent on anotherAsyncNotifierProvider
?Let's say I have a top level async notifier provider as below
Now if this provider is dependent on another
AsyncNotifierProvider
and want to fetch the data from the secondAsyncNotifierProvider
and also reacts to the changes of the secondAsyncNotifierProvider
, how do I write theasyncTodosProvider
? I could not find any example of this scenario.Beta Was this translation helpful? Give feedback.
All reactions