Created
January 30, 2023 08:47
-
-
Save Kurogoma4D/4a3c27a5e0c1ff2fe9b7863dbaef9b2f to your computer and use it in GitHub Desktop.
StreamProvider inspection.
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:riverpod/riverpod.dart'; | |
final originalStream = StreamProvider.family.autoDispose<String, String>((ref, prefix) { | |
return Stream.value('$prefix foo'); | |
}); | |
final prefixStore = StateProvider.autoDispose<String?>((_) => null); | |
final originalListener = StreamProvider.autoDispose<String>((ref) { | |
final prefix = ref.watch(prefixStore); | |
if (prefix == null) { | |
return const Stream.empty(); | |
} | |
return ref.watch(originalStream(prefix).stream); | |
}); | |
void main() async { | |
final container = ProviderContainer(); | |
final value = await container.read(originalStream('#').future); | |
print('first value: $value'); | |
container.listen( | |
originalListener, | |
(_, value) { | |
print('$value'); | |
}, | |
fireImmediately: true, | |
); | |
container.read(prefixStore.notifier).state = '#'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment