【问题标题】:I want to order my firestore data by its value我想按其值订购我的 Firestore 数据
【发布时间】:2020-08-19 15:50:40
【问题描述】:

我在 Firestore 上有多个数据。我想收到它们并按从小到大的顺序展示。

@override
void initState() {
Firestore.instance
    .collection("skorlar")
    .getDocuments()
    .then((QuerySnapshot snapshot) {
  snapshot.documents.forEach((f) {
    for(int i=0; i<(f.data.length)/2;i++){
      skor = f.data["skor"];
      skorListesi.add(skor);
    }
  });
});

Firestore.instance
    .collection("skorlar")
    .getDocuments()
    .then((QuerySnapshot snapshot) {
  snapshot.documents.forEach((f) {
    for(int i=0; i<(f.data.length)/2;i++){
      kullaniciAdi = f.data["user"];
      isimListesi.add(kullaniciAdi);
    }
  });
});

super.initState();
}

我在 1 个文档中有 2 个变量。这就是我的数据在列表中的样子。

Okan Altun 25(分)

xxx xxx 15 (pts)

yyy yyy 50 (pts)

我想让它们看起来像这样:

yyy yyy 50 (pts)

Okan Altun 25(分)

xxx xxx 50(分)

我可以使用排序功能对分数列表进行排序,但无法匹配其用户名。

   skorListesi.sort();
   skorListesi = skorListesi.reversed.toList();

这是我的列表视图生成器小部件:

Widget tekSatirSkor(String resim,String isim,int skor){
  return Padding(
    padding: EdgeInsets.all(30),
    child: Material(
      elevation: 15,
      borderRadius: BorderRadius.circular(15.0),
      child: Container(
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(15.0),
          color: Colors.white
        ),
        height: 75,
      child: Row(
        children: [
          Padding(
            padding: const EdgeInsets.fromLTRB(20.0,0.0,0.0,0.0),
            child: CircleAvatar(
              backgroundColor: Colors.white,
              backgroundImage: AssetImage(resim),
            ),
          ),
          Padding(
            padding: const EdgeInsets.fromLTRB(15.0,0.0,0.0,0.0),
            child: Text(isim,style: TextStyle(
              color: Colors.black,
              fontFamily: "GravityLight",
              fontSize: 24.0,
            ),),
          ),
        Padding(
          padding: EdgeInsets.fromLTRB(75.0, 0.0, 0.0, 0.0),
          child: Text(
            skor.toString(),
            style: TextStyle(
              color: Colors.black,
              fontFamily: "GravityLight",
              fontSize: 24.0,
            ),
          ),
        ),
        ],
      ),
      ),
    ),
  );
}

还有我的脚手架:

return Scaffold(
  body: ListView.builder(
    itemCount: skorListesi.length,
    itemBuilder: (context,i){
      return tekSatirSkor("images/award.png",isimListesi[i] , skorListesi[i]);
    },
  ),
);

请帮助我,这是我的应用程序中的最后一个问题。之后我会完成我的应用程序。

【问题讨论】:

  • 我不明白您想如何对列表进行排序。能否进一步澄清?
  • @WilsonWilson 得分最高。从大到小排序。
  • 所以你想根据数字/skor对它们进行排序?

标签: flutter google-cloud-firestore


【解决方案1】:

如果 "skor" 是您的 Firestore 集合中的一个单独字段,那么您可以直接从查询本身进行排序。

你能试试这个.orderBy("skor", descending: true)吗?
例如

Firestore.instance
    .collection("skorlar")
    .orderBy("skor", descending: true)
    .getDocuments()
...

【讨论】:

  • 谢谢 ağabey,这是你第二次帮助我。我很感激。
  • 不客气,stackoverflow 是很好协作的地方。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-18
  • 2021-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多