Flutter Firebase StreamBuilder 的 Orderby 可选值

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

Flutter Firebase Streambuilder Orderby optinal value

问题

StreamBuilder(stream: FirebaseFirestore.instance
.collection("notifications")
.doc(FirebaseAuth.instance.currentUser?.uid)
.collection("user-notifications")
.orderBy("timestamp", descending: true)
.limit(20).snapshots()

上述是Firebase查询,其中一些文档存在postId值,而其他文档不存在。是否可以在不使用".orderBy("postId", descending: true)"的情况下查询这个函数?我希望在我的listview.builder中使postId字段成为可选的。

我想要能够不使用postId进行排序,但我遇到了一个错误。如果在该集合中有20个文档,我希望在我的listview.builder中都能看到它们,有些有postId,有些没有postId。

目前,如果我移除按postId排序的筛选器,对于一些文档,我会得到"该字段不存在的问题"。如果保留它,我就看不到没有postId字符串的文档。

两个示例文档:

  • postId: XYZ, notificationId: XYY, uid: XCC
  • notificationId: ABC, , uid: ABD

<details>
<summary>英文:</summary>


StreamBuilder(stream: FirebaseFirestore.instance
.collection("notifications")
.doc(FirebaseAuth.instance.currentUser?.uid)
.collection("user-notifications")
.orderBy("timestamp", descending: true)
.orderBy("postId",descending: true)
.limit(20).snapshots()


I have the above Firebase query where postId value does exist for some documents and does not exist for others. Is it possible to query this function without &quot;.orderBy(&quot;postId&quot;, descending: true)&quot;? I want the post Id field to be optional in my listview.builder.

I&#39;d like to be able to order without the postId but I am getting an error. If I have 20 documents in that collection, I want to see them all in my listview.builder with and without postId.

At the moment, if I remove that order by postId filter, I get &quot;the field does not exist issue&quot; for some documents. if I keep it, I don&#39;t see the documents without the postId String.

2 example documents:
- postId: XYZ, notificationId: XYY, uid: XCC
- notificationId: ABC, , uid: ABD

</details>


# 答案1
**得分**: 0

Sure, here are the translations:

1. 是不是可以在不使用“.orderBy(“postId”,descending: true)”的情况下查询此函数?
   - 可以,但您将失去第二个排序。

2. 我想要在我的listview.builder中使帖子Id字段变成可选的。
   - 您不能使用可选字段来查询Firestore集合。您只能返回具有进行排序的字段的文档。

3. 如果我在该集合中有20个文档,我想在我的listview.builder中看到它们所有的文档,有的有postId,有的没有。
   - 在这种情况下,您需要删除对`.orderBy(“postId”,descending: true)`的调用,并在客户端执行排序。

4. 目前,如果我删除按postId筛选的排序,对一些文档会出现“字段不存在的问题”。
   - 而不是拥有不具有该字段的文档,您可以创建具有空字符串的字段。在这种情况下,您将不会收到任何错误。

<details>
<summary>英文:</summary>

&gt; Is it possible to query this function without &quot;.orderBy(&quot;postId&quot;, descending: true)&quot;?

Yes, it is possible but you&#39;ll lose the second order.

&gt; I want the post Id field to be optional in my listview.builder.

You cannot query a Firestore collection using an optional field. You can only return documents that do have the field on which you&#39;re performing the ordering.

&gt; If I have 20 documents in that collection, I want to see them all in my listview.builder with and without postId.

In this case, you have to remove the call to `.orderBy(&quot;postId&quot;,descending: true)` and perform the ordering on the client.

&gt; At the moment, if I remove that order by postId filter, I get &quot; the field does not exist issue&quot; for some documents.

Instead of having documents that do not have that field, you can create the field with an empty string. In this case you&#39;ll don&#39;t any errors.

</details>



huangapple
  • 本文由 发表于 2023年6月6日 04:43:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76409878.html
匿名

发表评论

匿名网友

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

确定