watchItems<T extends StorableModel> method
Watches for changes in the data store associated with a specific tag
and provides a stream of items of type T.
This method loads the initial data corresponding to the provided tag,
adds it to the stream controller, and returns a Stream that emits
a list of objects of type T whenever data changes. The transformation
is done by converting each entry from a Map<String, dynamic> 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.fromJson: A function that converts aMap<String, dynamic>to an object of typeT.
Returns:
- A
Future<Stream<List<T>>>that emits updated lists of items of typeT.
Throws:
- Any errors encountered during the initial data load will propagate.
Implementation
Future<Stream<List<T>>> watchItems<T extends StorableModel>(
final String tag,
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) =>
data.values
.map(
(final dynamic item) => fromJson(item as Map<String, dynamic>),
)
.toList(),
);
}