Skip to content

Commit d34bb1b

Browse files
committed
Fix DynamicStreamWidget to use watchValue instead of constructor param
The previous example used a constructor parameter with WatchingWidget, which wouldn't properly re-evaluate when the parameter changes. Now uses watchValue to get a dynamic value from the manager, which is a realistic use case for allowStreamChange: true. Changes: - Added useStream1 ValueNotifier to StreamManager - Updated DynamicStreamWidget to use watchValue to get the selection - Removed constructor parameter that wouldn't work correctly
1 parent 98f7388 commit d34bb1b

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

code_samples/lib/watch_it/_shared/stubs.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,9 +627,13 @@ class StreamManager {
627627
Stream<int> get stream1 => _controller1.stream;
628628
Stream<int> get stream2 => _controller2.stream;
629629

630+
// Controls which stream to use
631+
final useStream1 = ValueNotifier<bool>(true);
632+
630633
void dispose() {
631634
_controller1.close();
632635
_controller2.close();
636+
useStream1.dispose();
633637
}
634638
}
635639

code_samples/lib/watch_it/multiple_values_inline_combine_safe.dart

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,15 @@ class UnsafeInlineCombineWidget extends WatchingWidget {
7979

8080
// #region when_to_use_allow_change
8181
class DynamicStreamWidget extends WatchingWidget {
82-
const DynamicStreamWidget({super.key, required this.useStream1});
83-
84-
final bool useStream1;
82+
const DynamicStreamWidget({super.key});
8583

8684
@override
8785
Widget build(BuildContext context) {
88-
// ✅ CORRECT use of allowObservableChange: true
89-
// Stream actually changes based on widget parameter
86+
// Get dynamic value that determines which stream to watch
87+
final useStream1 = watchValue((StreamManager m) => m.useStream1);
88+
89+
// ✅ CORRECT use of allowStreamChange: true
90+
// Stream identity changes when useStream1 changes
9091
final data = watchStream(
9192
(StreamManager m) => useStream1 ? m.stream1 : m.stream2,
9293
initialValue: 0,

0 commit comments

Comments
 (0)