watchItem<T extends StorableModel> method
Watches for changes to a specific item in the data store.
This method loads the initial data for the provided tag, adds it to the
stream controller, and returns a Stream that emits an object of type T
whenever the specified item changes. If the item does not exist, null
is emitted.
The transformation is performed using the provided fromJson function.
Type Parameters:
T: A type extendingStorableModelthat represents the model of the data.
Parameters:
tag: AStringrepresenting the category or group of data to monitor.id: AStringrepresenting the unique identifier of the item to watch.fromJson: A function that converts aMap<String, dynamic>to an object of typeT.
Returns:
- A
Future<Stream<T?>>that emits the updated item of typeTornullif not found.
Throws:
- Any errors encountered during the initial data load will propagate.
Implementation
Future<Stream<T?>> watchItem<T extends StorableModel>(
final String tag,
final String id,
final T Function(Map<String, dynamic>) fromJson,
) async {
// Initial load
await _loadData(tag).then((final Map<String, dynamic> data) {
_getController(tag).add(data);
});
return _getController(tag).stream.map((final Map<String, dynamic> data) {
final dynamic itemData = data[id];
if (itemData == null) {
return null;
}
return fromJson(itemData as Map<String, dynamic>);
});
}