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

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

问题描述

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

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:

questionsView:"3",questionsView:"1032"等

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

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

但这都不能正确执行排序.

But neither does this do the ordering correctly.

我从传奇人物弗兰克·冯·帕芬(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 属性将整数保存为字符串值,实际上这是同一回事.在这种情况下,解决方案是将属性的类型更改为number或填充数字,以便它们都具有相同的长度,但是在您的情况下,属性的类型应明确地定义为 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订购数据,订购不正确订购的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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