search method

Future<SearchResults> search(
  1. Categories searchCategories, {
  2. String? nextBatch,
})

Performs a full text search across different categories.

nextBatch The point to return events from. If given, this should be a next_batch result from a previous call to this endpoint.

searchCategories Describes which categories to search in and their criteria.

Implementation

Future<SearchResults> search(Categories searchCategories,
    {String? nextBatch}) async {
  final requestUri = Uri(path: '_matrix/client/v3/search', queryParameters: {
    if (nextBatch != null) 'next_batch': nextBatch,
  });
  final request = Request('POST', baseUri!.resolveUri(requestUri));
  request.headers['authorization'] = 'Bearer ${bearerToken!}';
  request.headers['content-type'] = 'application/json';
  request.bodyBytes = utf8.encode(jsonEncode({
    'search_categories': searchCategories.toJson(),
  }));
  final response = await httpClient.send(request);
  final responseBody = await response.stream.toBytes();
  if (response.statusCode != 200) unexpectedResponse(response, responseBody);
  final responseString = utf8.decode(responseBody);
  final json = jsonDecode(responseString);
  return SearchResults.fromJson(json as Map<String, Object?>);
}