我只想显示10个文档,这些文档按数字OfLikes降序排列,并且是在过go 24小时内创建的.但是每当我使用where()和orderBy()时,逻辑就失败了.运行下面的代码后,我添加了所需的Firebase索引,但文档是按最近发布的而不是最受欢迎的进行排序的.这样不可能吗?如果是这样的话,我怎么才能做到这一点呢?注:number OfLikes是FireStore数据库中的一个数字.如果有任何帮助,我将不胜感激.谢谢.
StreamBuilder(
stream: FirebaseFirestore.instance
.collection('posts')
.where("datePublished",
isGreaterThan:
DateTime.now().subtract(const Duration(hours: 24)))
.orderBy("datePublished", descending: true)
.orderBy("numberOfLikes", descending: true)
.limit(10)
.snapshots(),
builder: (context,
AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>> snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(child: CircularProgressIndicator());
}
if (snapshot.hasError) {
print(snapshot.error);
return Text("No Posts Found");
}
return Container(
height: MediaQuery.of(context).size.height * .37,
width: MediaQuery.of(context).size.width * .98,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: snapshot.data!.docs.length,
itemBuilder: (context, index) => TrendingPostsCard(
snap: snapshot.data!.docs[index].data(),
),
),
);
},
),