next method

Implementation

@override
Future<SearchResult?> next() async {
  if (fetched >= total!) {
    return null;
  }

  if (request!.scroll != null && request!.scroll!.isNotEmpty) {
    final query = KuzzleRequest(
      controller: controller,
      scrollId: response!.result['scrollId'] as String?,
    );
    query.action = scrollAction;
    query.lang = lang;
    return await kuzzle
        .query(query)
        .then((_response) {
      response = _response;

      final result = response!.result as Map<String, dynamic>;

      if (result.containsKey('aggregations')) {
        aggregations = result['aggregations'] as Map<String, dynamic>?;
      }
      if (result.containsKey('hits')) {
        hits = result['hits'] as List<dynamic>?;
        fetched = fetched;
      }

      return buildNextSearchResult(response);
    });
  } else if (request!.size != null && request!.sort != null) {
    final _request = KuzzleRequest.clone(request!)
      ..action = searchAction
      ..lang = lang;

    _request.body ??= <String, dynamic>{};
    _request.body!['search_after'] ??= <dynamic>[];

    final hit = hits!.last;

    for (var sort in request!.sort!) {
      final key =
          (sort is String) ? sort : (sort as Map<String, dynamic>).keys.first;
      final value = (key == '_uid')
          ? '${request!.collection}#${hit['_id']}'
          : _get(hit['_source'] as Map<String, dynamic>?, key.split('.'));

      _request.body!['search_after'].add(value);
    }

    return await kuzzle.query(_request).then((_response) {
      response = _response;

      final result = response!.result as Map<String, dynamic>;

      if (result.containsKey('aggregations')) {
        aggregations = result['aggregations'] as Map<String, dynamic>?;
      }
      if (result.containsKey('hits')) {
        hits = result['hits'] as List<dynamic>?;
        fetched = hits!.length;
      }

      return buildNextSearchResult(response);
    });
  } else if (request!.size != null) {

    if (request!.from! >= total!) {
      return null;
    }

    return await kuzzle
        .query(KuzzleRequest.clone(request!)
          ..action = searchAction
          ..from = fetched)
        .then((_response) {
      response = _response;

      final result = response!.result as Map<String, dynamic>;

      if (result.containsKey('aggregations')) {
        aggregations = result['aggregations'] as Map<String, dynamic>?;
      }
      if (result.containsKey('hits')) {
        hits = result['hits'] as List<dynamic>?;
        fetched = fetched;
      }

      return buildNextSearchResult(response);
    });
  }

  throw KuzzleError('Unable to retrieve next results from search: '
      'missing scrollId, from/sort, or from/size params');
}