0

我想做一个查询,如果它符合一个条件,我想把这个查询作为下一个查询。有人知道我该怎么做吗?

我的目标是在客户端上执行此操作,因为没有非数组查询

Query _query(List<dynamic> startAfter) {
    CollectionReference col = _firestore.collection("users");
    Query xx= col.where('xx', whereIn: _xx);
    Query yy = xx.where('yy', arrayContains: _yy);
    Query zz = yy.orderBy('zz');
    Query start;
    if (startAfter.length > 0) {
      start = zz.startAfter(startAfter);
    } else {
      start = zz;
    }
    Query limit = start.limit(1);
    return limit;
  }

我想过这样的事情,但我想我做错了

_query([]).getDocuments().then((onValue) {
            if (onValue.documents.length > 0) {
              if (_all.contains(onValue.documents.first.documentID)) {
                List<DocumentSnapshot> doc = onValue.documents;
                String id = doc.first.documentID;
                do {
                  _query(doc).getDocuments().then((onValue) {
                    doc = onValue.documents;
                    id = doc.first.documentID;
                  });
                }
                while(_all.contains(id) == false);
                if(doc.length > 0) {
                  debugPrint('found');
                } else {
                  debugPrint('could not find');
                }
              } else {
                debugPrint('found');
              }
            } else {
              debugPrint('could not find');
            }
4

1 回答 1

1

你可以用 await 那样做

Future<QuerySnapshot> _query([DocumentSnapshot startAfter]) {
    CollectionReference col = _firestore.collection("users");
    Query xx= col.where('xx', whereIn: _xx);
    Query yy = xx.where('yy', arrayContains: _yy);
    Query zz = yy.orderBy('zz');
    Query start;
    if (startAfter != null) {
      start = order.startAfterDocument(startAfter);
    } else {
      start = order;
    }
    Query limit = start.limit(1);
    return limit.getDocuments();
  }

后:

  QuerySnapshot snap = await _query();
  if (snap.documents.length > 0) {
    if (_all.contains(onValue.documents.first.documentID)) {
      List<DocumentSnapshot> doc = snap.documents;
      while (doc.length > 0 && _all.contains(doc.first.documentID) == false) {
        QuerySnapshot snap2 = await _query(doc.first);
        doc = snap2.documents;
      }
      if (doc.length > 0) {
        debugPrint('found');
      } else {
        debugPrint('could not find');
      }
    } else {
      debugPrint('found');
    }
  } else {
    debugPrint('could not find');
  }
于 2020-02-10T11:40:43.117 回答