ItemListBloc<I extends ItemClassWithAccessor, T extends ItemSourceState<ItemClassWithAccessor>> constructor

ItemListBloc<I extends ItemClassWithAccessor, T extends ItemSourceState<ItemClassWithAccessor>>({
  1. required FilterConditionsBloc<ItemSourceState<ItemClassWithAccessor>> filterConditionsBloc,
  2. required SearchQueryCubit searchQueryCubit,
  3. required Bloc sourceBloc,
  4. List<String> searchProperties = const [],
})

Attaches to the provided _filterConditionsBloc, _searchQueryCubit, and _sourceBloc and uses supplied _searchProperties in order to generate a list of items that should be rendered to the UI.

The active conditions from the supplied _filterConditionsBloc are additive, so items matching any of the active conditions will be returned. Once the source items have been filtered, the search query will be applied to any remaining items to generate the final list state.

There should be no need to ever manually construct an ItemListBloc. It should, instead, be retrieved from within the ListManager in order to render your list UI however you see fit.

Implementation

ItemListBloc({
  required FilterConditionsBloc filterConditionsBloc,
  required SearchQueryCubit searchQueryCubit,
  required Bloc sourceBloc,
  List<String> searchProperties = const [],
})  : _filterConditionsBloc = filterConditionsBloc,
      _searchQueryCubit = searchQueryCubit,
      _sourceBloc = sourceBloc,
      _searchProperties = searchProperties,
      super(const NoSourceItems()) {
  _filterConditionsSubscription = _filterConditionsBloc.stream.listen((_) {
    add(_ExternalDataUpdated());
  });

  _searchQuerySubscription = _searchQueryCubit.stream.listen((_) {
    add(_ExternalDataUpdated());
  });

  _sourceSubscription = _sourceBloc.stream.listen((_) {
    add(_ExternalDataUpdated());
  });

  on<_ExternalDataUpdated>((event, emit) {
    if (_filterConditionsBloc.state is! ConditionsInitialized ||
        _sourceBloc.state is! T) {
      return emit(const NoSourceItems());
    }

    final filterResults = _filterSource(_sourceBloc.state.items);
    final searchResults =
        _searchSource(_searchQueryCubit.state, filterResults);

    return emit(searchResults.isEmpty
        ? const ItemEmptyState()
        : ItemResults(searchResults.toList()));
  });
}