英文:
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 ".orderBy("postId", descending: true)"? I want the post Id field to be optional in my listview.builder.
I'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 "the field does not exist issue" for some documents. if I keep it, I don'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>
> Is it possible to query this function without ".orderBy("postId", descending: true)"?
Yes, it is possible but you'll lose the second order.
> 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're performing the ordering.
> 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("postId",descending: true)` and perform the ordering on the client.
> At the moment, if I remove that order by postId filter, I get " the field does not exist issue" 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'll don't any errors.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论