简体   繁体   中英

I want to order my firestore data by its value

I have multiple data on firestore. I want to receive them and show in order of small to large.

@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();
}

I have 2 variables in 1 document. This is how my data looks like in list.

Okan Altun 25 (pts)

xxx xxx 15 (pts)

yyy yyy 50 (pts)

And I want to make them look like this:

yyy yyy 50 (pts)

Okan Altun 25 (pts)

xxx xxx 50 (pts)

I can sort score list with sort function but can't match up its user name.

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

This is my widget for listview builder:

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,
            ),
          ),
        ),
        ],
      ),
      ),
    ),
  );
}

And my scaffold:

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

Please help me, this is the last problem in my app. I will finish my app after this.

If "skor" is a separate field in your Firestore collection, then you can do sorting right from the query itself.

Can you please try using this .orderBy("skor", descending: true) ?
for example

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM