operator method

void operator(
  1. QueryOperator operator
)
  • AND Operator Get entries that satisfy all the conditions provided in the '$and' query.

  • OR Operator Get all entries that satisfy at least one of the given conditions provided in the '$or' query.

  • {Example}: operator OR

final stackInstance1 = Credential.stack();
final queryBase1 = stackInstance1.contentType('room').entry().query();
queryBase1.where('title', QueryOperation.equals(value: 'Room 13'));
final stackInstance2 = Credential.stack();
final queryBase2 = stackInstance2.contentType('room').entry().query();
queryBase2.where('attendee', QueryOperation.equals(value: 20));
final List<contentstack.Query> listOfQuery = [queryBase1, queryBase2];
query.operator(QueryOperator.or(queryObjects: listOfQuery));
await query.find().then((response){
   print(response.toString());
}).catchError((onError){
   print(onError);
});
  • {Example}: And Operator:
final stackInstance1 = Credential.stack();
final queryBase1 = stackInstance1.contentType('room').entry().query();
queryBase1.where('title', QueryOperation.equals(value: 'Room 13'));
final stackInstance2 = Credential.stack();
final queryBase2 = stackInstance2.contentType('room').entry().query();
queryBase2.where('attendee', QueryOperation.equals(value: 20));
final List<contentstack.Query> listOfQuery = [queryBase1, queryBase2];
query.operator(QueryOperator.and(queryObjects: listOfQuery));
await query.find().then((response){
   print(response.toString());
}).catchError((onError){
   print(onError);
});

Implementation

void operator(QueryOperator operator) {
  operator.when(and: (and) {
    final List<Query> queryList =
        and.queryObjects; //and.queryObjects is list of Query Objects
    if (queryList.isNotEmpty) {
      final emptyList = [];
      for (final item in queryList) {
        emptyList.add(item.parameter);
      }
      parameter['\$and'] = emptyList;
    }
  }, or: (or) {
    final List<Query> queryList =
        or.queryObjects; //and.queryObjects is list of Query Objects
    if (queryList.isNotEmpty) {
      final emptyList = [];
      for (final item in queryList) {
        emptyList.add(item.parameter);
      }
      parameter['\$or'] = emptyList;
    }
  });
}