queryChannelsOnline method

Future<List<Channel>> queryChannelsOnline(
  1. {Filter? filter,
  2. List<SortOption<ChannelModel>>? sort,
  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()}
)

Requests channels with a given query from the API.

Implementation

Future<List<Channel>> queryChannelsOnline({
  Filter? filter,
  List<SortOption<ChannelModel>>? sort,
  bool state = true,
  bool watch = true,
  bool presence = false,
  int? memberLimit,
  int? messageLimit,
  bool waitForConnect = true,
  PaginationParams paginationParams = const PaginationParams(),
}) async {
  if (waitForConnect) {
    if (_ws.connectionCompleter?.isCompleted == false) {
      logger.info('awaiting connection completer');
      await _ws.connectionCompleter?.future;
    }
    if (wsConnectionStatus != ConnectionStatus.connected) {
      throw const StreamChatError(
        'You cannot use queryChannels without an active connection. '
        'Please call `connectUser` to connect the client.',
      );
    }
  }

  if (!_connectionIdManager.hasConnectionId) {
    // ignore: parameter_assignments
    watch = false;
  }

  logger.info('Query channel start');
  final res = await _chatApi.channel.queryChannels(
    filter: filter,
    sort: sort,
    state: state,
    watch: watch,
    presence: presence,
    memberLimit: memberLimit,
    messageLimit: messageLimit,
    paginationParams: paginationParams,
  );

  if (res.channels.isEmpty && paginationParams.offset == 0) {
    logger.warning('''
      We could not find any channel for this query.
      Please make sure to take a look at the Flutter tutorial: https://getstream.io/chat/flutter/tutorial
      If your application already has users and channels, you might need to adjust your query channel as explained in the docs https://getstream.io/chat/docs/query_channels/?language=dart
      ''');
    return <Channel>[];
  }

  final channels = res.channels;

  final users = channels
      .expand((it) => it.members)
      .map((it) => it.user)
      .toList(growable: false);

  this.state.updateUsers(users);

  logger.info('Got ${res.channels.length} channels from api');

  final updateData = _mapChannelStateToChannel(channels);

  await _chatPersistenceClient?.updateChannelQueries(
    filter,
    channels.map((c) => c.channel!.cid).toList(),
    clearQueryCache: paginationParams.offset == 0,
  );

  this.state.channels = updateData.key;
  return updateData.value;
}