如何在 Flutter 中从 Firestore 订购数据,orderBy 订购不正确 [英] How to order data from Firestore in Flutter, orderBy not ordering correct

查看:27
本文介绍了如何在 Flutter 中从 Firestore 订购数据,orderBy 订购不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个应用程序,用户可以在其中写入我的数据库并向其他用户提问,例如医生应用程序.医生可以在哪里回答问题.

I'm building an app where the users can write into my db and ask questions to other users, like a doctor app. Where the doctors can answer the questions.

我想按日期排序问题,我想按最新和最受欢迎的问题排序.

I wanna order the questions by date, where I would like to order the questions by newest, and also the most popular.

我尝试通过以下方式订购最新的:

I try to order the newest by the following:

日期:2019 年 9 月 29 日"

date: "29 September 2019"

Firestore.instance
        .collection("Questions")
        .orderBy("date", descending: true) // 1 will be last, 31 will be latest
        .snapshots(),

但这似乎不起作用.所以我想知道如何从 firebase 的查询中正确排序数据.或者我是否必须在 Firebase 中将日期存储为很长的时间,然后再进行转换?

But that seem not to work. So I wonder how I could order the data correctly from my query from firebase. Or do I have to store the date as a long in the firebase and then convert it maybe?

此外,我想根据以下方式的观看特定问题的用户数量对最受欢迎的问题进行排序:

Also, I would like to order the most popular questions, based on how many users have watched that particular questions by:

问题视图:3",问题视图:1032"等

questionsView: "3", questionsView: "1032" etc

Firestore.instance
        .collection("Questions")
        .orderBy("questionsView")
        .snapshots(),

但这也不能正确排序.

我看到了传奇人物弗兰克·冯·普芬 (Frank von Puffen) 的一篇很棒的帖子,但我不太明白,所以我想知道是否有人可以帮我解决这个问题.

I saw a great post from the legendery Frank von Puffen but I didn't really understand that, so I wonder if somebody could help me out on this.

这是他的帖子:在 FireStore 中订购数据

最好的问候,鲁斯本

推荐答案

我尝试通过以下方式订购最新的:

I try to order the newest by the following:

日期:2019 年 9 月 29 日"

date: "29 September 2019"

但这似乎不起作用.

它不会工作,因为您的 date 属性是 String 类型.对字符串进行排序时,请记住字符串是按词典排序的.

It won't work since your date property is of type String. When ordering strings, remember that the strings are ordered lexicographically.

所以我想知道如何从来自 firebase 的查询中正确排序数据.

So I wonder how I could order the data correctly from my query from firebase.

要解决这个问题,您应该将您的属性类型更改为 Date.在这种情况下,您会得到一个时间顺序.

To solve this, you should change the type of you property to be a Date. In this case you'll get a chronological order.

或者我是否必须在 Firebase 中将日期存储为很长的时间,然后再进行转换?

Or do I have to store the date as a long in the firebase and then convert it maybe?

这是一种用于对元素进行排序的解决方案,适用于实时数据库,适用于 Cloud Firestore.

That's a solution for ordering elements is for realtime database and not for Cloud Firestore.

我看到了传奇人物弗兰克·冯·普芬 (Frank von Puffen) 的一篇很棒的帖子,但我不太明白,所以我想知道是否有人可以帮我解决这个问题.

I saw a great post from the legendery Frank von Puffen but I didn't really understand that, so I wonder if somebody could help me out on this.

在那种情况下,station 属性将整数作为字符串值保存,这实际上是相同的.在这种情况下,解决方案是将属性的类型更改为数字或填充数字,以便它们都具有相同的长度,但在您的情况下,您的属性类型应明确为 Date不是字符串.

In that case, the station property was holding integers as string values, which in fact is the same thing. In that case, the solution was to change the type of the property to number or to pad the numbers so they all have the same length but in your case, the type of your property should definetely be Date and not a String.

这篇关于如何在 Flutter 中从 Firestore 订购数据,orderBy 订购不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆