合并两个 Future<QuerySnapshots>

huangapple go评论64阅读模式
英文:

Merge two Future<QuerySnapshots>

问题

需要合并两个查询并返回一个Future<QuerySnapshot>。我的代码没有将query2合并到query1中,只返回了query1中的文档。以下是代码:

Future<QuerySnapshot> mergeQueries() async {
    QuerySnapshot query1 = await FirebaseFirestore.instance
        .collection('practice')
        .orderBy('timeStamp', descending: true)
        .where('teamId', isEqualTo: widget.teamId)
        .where('checkSinglesDoubles', isEqualTo: dropDownType)
        .where('player1name', isEqualTo: playerNameFilter)
        .get();

    QuerySnapshot query2 = await FirebaseFirestore.instance
        .collection('practice')
        .orderBy('timeStamp', descending: true)
        .where('teamId', isEqualTo: widget.teamId)
        .where('checkSinglesDoubles', isEqualTo: dropDownType)
        .where('player2name', isEqualTo: playerNameFilter)
        .get();

    query1.docs.addAll(query2.docs);

    return query1;
}
英文:

I need to merge two queries into one and return a Future&lt;QuerySnapshot&gt;. My code is not merging query2 into query1, it is only returning docs in query1. Code below:

Future&lt;QuerySnapshot&gt; mergeQueries() async {
    QuerySnapshot query1 = await FirebaseFirestore.instance
        .collection(&#39;practice&#39;)
        .orderBy(&#39;timeStamp&#39;, descending: true)
        .where(&#39;teamId&#39;, isEqualTo: widget.teamId)
        .where(&#39;checkSinglesDoubles&#39;, isEqualTo: dropDownType)
        .where(&#39;player1name&#39;, isEqualTo: playerNameFilter)
        .get();

    QuerySnapshot query2 = await FirebaseFirestore.instance
        .collection(&#39;practice&#39;)
        .orderBy(&#39;timeStamp&#39;, descending: true)
        .where(&#39;teamId&#39;, isEqualTo: widget.teamId)
        .where(&#39;checkSinglesDoubles&#39;, isEqualTo: dropDownType)
        .where(&#39;player2name&#39;, isEqualTo: playerNameFilter)
        .get();

    query1.docs.addAll(query2.docs);

    return query1;
    }

答案1

得分: 1

The docs property of a QuerySnapshot object is read-only, so calling query1.docs.addAll(query2.docs) has no effect.

There is no way to combine two queries (or their corresponding query snapshots) into one. This makes sense - since on the server side Firestore does not support logically compound queries in general.

What you could do instead is to extract the documents (or the data they contain) from the QuerySnapshots and combine those with each other. This will change your return type, but provide the functionality you were looking for.

Future<QueryDocumentSnapshot<T>> mergeQueries() async {
  var query1 = await ...;
  var query2 = await ...;

  var combinedDocs = query1.docs;
  combinedDocs.addAll(query2.docs);
  return combinedDocs;
}
英文:

The docs property of a QuerySnapshot object is read-only, so calling query1.docs.addAll(query2.docs) has no effect.

There is no way to combine two queries (or their corresponding query snapshots) into one. This makes sense - since on the server side Firestore does not support logically compound queries in general.

What you could could do instead is to extract the documents (or the data they contain) from the QuerySnapshots and combine those with each other. This will change your return type, but provide the functionality you were looking for.

Future&lt;QueryDocumentSnapshot&lt;T&gt;&gt; mergeQueries() async {
  var query1 = await ...;
  var query2 = await ...;

  var combinedDocs = query1.docs;
  combinedDocs.addAll(query2.docs);
  return combinedDocs;
}

huangapple
  • 本文由 发表于 2023年2月27日 06:39:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75575427.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定