watchFilteredItems<T extends StorableModel> method

Future<Stream<List<T>>> watchFilteredItems<T extends StorableModel>(
  1. String tag,
  2. T fromJson(
    1. Map<String, dynamic>
    ),
  3. bool filter(
    1. T item
    )
)

Watches for changes to filtered items 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 a list of objects of type T whenever data changes. Items are filtered using the provided filter function, and only matching items are included in the emitted list. The transformation of data is handled using the fromJson function.

Type Parameters:

  • T: A type extending StorableModel that represents the model of the data.

Parameters:

  • tag: A String representing the category or group of data to monitor.
  • fromJson: A function that converts a Map<String, dynamic> to an object of type T.
  • filter: A predicate function that takes an item of type T and returns a bool. Items for which this function returns true are included in the stream.

Returns:

  • A Future<Stream<List<T>>> that emits filtered lists of items of type T.

Throws:

  • Any errors encountered during the initial data load will propagate.

Implementation

Future<Stream<List<T>>> watchFilteredItems<T extends StorableModel>(
  final String tag,
  final T Function(Map<String, dynamic>) fromJson,
  final bool Function(T item) filter,
) 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>),
            )
            .where(filter)
            .toList(),
  );
}