queryChannels method

Future<void> queryChannels({
  1. Filter? filter,
  2. List<SortOption<ChannelModel>>? sortOptions,
  3. bool state = true,
  4. bool watch = true,
  5. bool presence = false,
  6. int? memberLimit,
  7. int? messageLimit,
  8. bool waitForConnect = true,
  9. PaginationParams paginationParams = const PaginationParams(limit: 30),
})

Calls client.queryChannels updating queryChannelsLoading stream

Implementation

Future<void> queryChannels({
  Filter? filter,
  List<SortOption<ChannelModel>>? sortOptions,
  bool state = true,
  bool watch = true,
  bool presence = false,
  int? memberLimit,
  int? messageLimit,
  bool waitForConnect = true,
  PaginationParams paginationParams = const PaginationParams(limit: 30),
}) async {
  final client = _streamChatCoreState!.client;

  final offset = paginationParams.offset;
  final clear = offset == null || offset == 0;
  if (clear && _paginationEnded) {
    _paginationEnded = false;
  }

  if ((!clear && _paginationEnded) || _queryChannelsLoadingController.value) {
    return;
  }

  if (_channelsController.hasValue) {
    _queryChannelsLoadingController.safeAdd(true);
  }

  try {
    final oldChannels = List<Channel>.from(channels ?? []);
    var newChannels = <Channel>[];
    await for (final channels in client.queryChannels(
      filter: filter,
      sort: sortOptions,
      state: state,
      watch: watch,
      presence: presence,
      memberLimit: memberLimit,
      messageLimit: messageLimit,
      waitForConnect: waitForConnect,
      paginationParams: paginationParams,
    )) {
      newChannels = channels;
      if (clear) {
        _channelsController.safeAdd(channels);
      } else {
        final temp = oldChannels + channels;
        _channelsController.safeAdd(temp);
      }
      if (_channelsController.hasValue &&
          _queryChannelsLoadingController.value) {
        _queryChannelsLoadingController.safeAdd(false);
      }
    }
    if (newChannels.isEmpty || newChannels.length < paginationParams.limit) {
      _paginationEnded = true;
    }
  } catch (e, stk) {
    // reset loading controller
    _queryChannelsLoadingController.safeAdd(false);
    if (_channelsController.hasValue) {
      _queryChannelsLoadingController.safeAddError(e, stk);
    } else {
      _channelsController.safeAddError(e, stk);
    }
  }
}