compoundBody property Null safety

Map<String, dynamic> compoundBody
override

Implementation

Map<String, dynamic> get compoundBody {
  switch (_queryType) {
    case QueryType.IS_EQUAL_TO:
      {
        (_compoundQuery["query"]["bool"]["must"] as List).add(equalToBody);
      }
      break;
    case QueryType.IS_GREATER_THAN:
      {
        (_compoundQuery["query"]["bool"]["must"] as List)
            .add(comparisonBody("gt"));
      }
      break;
    case QueryType.IS_GREATER_THAN_OR_EQUAL_TO:
      {
        (_compoundQuery["query"]["bool"]["must"] as List)
            .add(comparisonBody("gte"));
      }
      break;
    case QueryType.IS_LESS_THAN:
      {
        (_compoundQuery["query"]["bool"]["must"] as List)
            .add(comparisonBody("lt"));
      }
      break;
    case QueryType.IS_LESS_THAN_OR_EQUAL_TO:
      {
        (_compoundQuery["query"]["bool"]["must"] as List)
            .add(comparisonBody("lte"));
      }
      break;
    case QueryType.CONTAINS:
      {
        (_compoundQuery["query"]["bool"]["must"] as List)
            .addAll(containsBody);
      }
      break;
    case QueryType.IS_WITHIN_RADIUS:
      {
        (_compoundQuery["query"]["bool"]["filter"] as List)
            .add(geoDistanceBody(_value));
      }
      break;
    case QueryType.IS_WITHIN_BOX:
      {
        (_compoundQuery["query"]["bool"]["filter"] as List)
            .add(geoBoxBody(_value));
      }
      break;
    case QueryType.START_AT:
      {
        _compoundQuery["from"] = _value;
      }
      break;
    case QueryType.START_AFTER:
      {
        final Document doc = _value;

        // doc.sort may have truncated the timestamp value is it is in nanoseconds range...
        // List searchAfter = doc.sort ?? [];

        // In order to search after a timestamp in the ns range, compute the nanosecondsSinceEpoch
        // from the field itself, not from the values returned by ES.
        List searchAfter = [];

        assert(doc.source != null,
            "Document provided to start after query must have a non-null source");
        try {
          for (Map<String, dynamic> sort in _compoundQuery["sort"]) {
            final String key = sort.keys.toList()[0];
            if (key == "_id") {
              searchAfter.add(doc.id);
              continue;
            }
            dynamic value = doc.source![key];
            // Convert to timestamp if its a time value
            try {
              value = this.nanoDate
                  ? TimeHelper.nanosecondsSinceEpoch(value)
                  : TimeHelper.millisecondsSinceEpoch(value);
            } catch (e) {}

            searchAfter.add(value);
          }
        } catch (e) {}

        _compoundQuery["search_after"] = searchAfter;
      }
      break;
    case QueryType.LIMIT:
      {
        _compoundQuery["size"] = _value;
      }
      break;
    case QueryType.ORDER_BY:
      {
        (_compoundQuery["sort"] as List).add({_field: _value});
        if (this.addId) (_compoundQuery["sort"] as List).add({"_id": "asc"});
      }
      break;
  }
  return _compoundQuery;
}