Last active
February 2, 2025 01:53
-
-
Save dickermoshe/47276292cb4c8dd7ba9507d4f914466c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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