Skip to content

Instantly share code, notes, and snippets.

@dickermoshe
Last active February 2, 2025 01:53
Show Gist options
  • Save dickermoshe/47276292cb4c8dd7ba9507d4f914466c to your computer and use it in GitHub Desktop.
Save dickermoshe/47276292cb4c8dd7ba9507d4f914466c to your computer and use it in GitHub Desktop.
import "package:rxdart/rxdart.dart";
void main() async {
// Emit an incrementing number every 5 seconds
final parentStream = Stream.periodic(Duration(seconds: 5), (i) => i);
/// Every second, emit an incrementing number which is multipled by the current parentStream
final childStream = parentStream.asyncMap((multiplyBy) {
return Stream.periodic(Duration(seconds: 1), (i) => i * multiplyBy);
});
// Use `switchMap` to get the latest values from switchMap
final listener = childStream.switchMap((i) => i).listen((i) {
print(i);
});
await listener.asFuture();
}
/// Using Riverpod
// @riverpod
// Stream<int> parent(Ref ref){
// return Stream.periodic(Duration(seconds:5),(i)=>i);
// }
// @riverpod
// Stream<int> child(Ref ref, int multiplyBy){
// return Stream.periodic(Duration(seconds:1),(i)=> i * multiplyBy);
// }
// final parentValue = await ref.watch(parentProvider.future);
// final childValue = ref.watch(childProvider(parentValue).future);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment