You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
**Note**: Unlike traditional streams, ItemHolder does **not** emit errors during value retrieval. This prevents
147
+
transient failures (like network issues) from being cached and replayed to future listeners. The stream simply
148
+
retains its last valid value and retries on the next update.
149
+
139
150
## Converting Item Holder to a ValueNotifier
140
151
141
152
You can convert an `ItemHolder` to a `ValueNotifier` for easier integration with Flutter's state management.
@@ -192,39 +203,24 @@ subscription.cancel();
192
203
193
204
## ⚠️ Important: Using Streams with Flutter's StreamBuilder
194
205
195
-
When using streams with Flutter's `StreamBuilder`, it's crucial to understand the difference between safe and unsafe patterns.
206
+
When using streams with Flutter's `StreamBuilder`, it's important to understand the best patterns and practices.
196
207
197
-
### ❌**Unsafe Pattern: Calling `stream()`directly in build method**
208
+
### ✅**The `stream()`Method is Now Safe**
198
209
199
-
**DO NOT do this:**
210
+
As of the latest version, the `stream()` method returns a **cached `ItemHolder` instance**, making it safe to call repeatedly:
200
211
201
212
```dart
202
-
class MyWidget extends StatelessWidget {
203
-
@override
204
-
Widget build(BuildContext context) {
205
-
return StreamBuilder<String?>(
206
-
stream: storage.stream<String>('name'), // ❌ BAD: Creates new stream every rebuild!
207
-
builder: (context, snapshot) {
208
-
return Text(snapshot.data ?? 'Unknown');
209
-
},
210
-
);
211
-
}
212
-
}
213
+
// These both return the exact same ItemHolder object:
214
+
final stream1 = storage.stream<String>('name');
215
+
final stream2 = storage.stream<String>('name');
216
+
print(identical(stream1, stream2)); // true
213
217
```
214
218
215
-
**Why this is problematic:**
219
+
This means calling `storage.stream('key')` directly in a build method is now technically safe, as it returns the same cached instance each time. However, for code clarity and best practices, we still recommend the patterns below.
216
220
217
-
- Every time `build()` is called, a **new stream instance** is created
218
-
-`StreamBuilder` detects a different stream and recreates the subscription
219
-
- This causes:
220
-
- Unnecessary memory allocations (new `StreamController` each time)
0 commit comments