Home > Software design >  How to fetch sorted data from firebase in flutter
How to fetch sorted data from firebase in flutter

Time:12-12

Here i want to sort my notes in the order in which i uploaded them. Here i used orderBy('Time') but that is not working and the data i am getting is not in order of time. And i am not getting how this orderBy function is doing here. Please refer me some document for this also. Thanks in advance


 FutureBuilder<QuerySnapshot>(
             ***future:ref.orderBy('Time').get(),***
                builder: (context,snapshot){
                if(snapshot.hasData){
                  return ListView.builder(
                    physics: ClampingScrollPhysics(),
                    padding: EdgeInsets.fromLTRB(2, 2, 2, 2),
                       shrinkWrap: true,
                      itemCount: snapshot.data!.docs.length,
                      itemBuilder: (context,index){
                      Map data= snapshot.data!.docs[index].data() as Map;
                      DateTime mynote_time=data['Time'].toDate();
                      String formattedTime =
                      DateFormat.yMMMd().add_jm().format(mynote_time);
                      return Column(
                        children: [
                          Card(
                            child: Column(
                              children: [
                                Padding(
                                  padding: const EdgeInsets.fromLTRB(3, 1, 3, 1),
                                  child: Text(
                                      "${data['title']}",
                                    style: TextStyle(
                                      fontWeight: FontWeight.w700,
                                      fontSize: 20,
                                    ),
                                  ),
                                ),
                                Padding(
                                  padding: const EdgeInsets.fromLTRB(3, 1, 3, 1),
                                  child: Text(
                                    "${data['description']}",
                                    style: GoogleFonts.lato(
                                      fontSize: 14,
                                    ),
                                  ),
                                ),
                                Container(
                                  alignment: Alignment.bottomRight,
                                  child: Text(
                                    formattedTime
                                  ),
                                )
                              ],
                            ),
                            borderOnForeground: true,
                            shadowColor: Colors.blueAccent,
                            color: Colors.grey[100],
                            elevation: 10,
                          ),
                        ],
                      );
                      }
                      );
                }
                else if(snapshot==null){
                  return Text('Please Enter some notes');
                }
                else return Padding(
                  padding: EdgeInsets.fromLTRB(150, 200, 0, 50),
                  child: SpinKitPumpingHeart(
                    color: Colors.blueAccent,
                    size: 100,
                    duration: Duration(seconds: 2),
                  ),
                );
                }),

Below code is the function which is sending my data to firebase.

void addnote() async{
    CollectionReference ref= FirebaseFirestore.instance
        .collection("users")
        .doc(FirebaseAuth.instance.currentUser!.uid)
        .collection('Notes');

    var data={
      'title': titletext.text,
      'description': destext.text,
      'Time': DateTime.now(),
    };
    ref.add(data);

  }

CodePudding user response:

firestore provides an function for that its called orderBy. It should look like this:

Firestore.instance
     .collection("users")
     .doc(FirebaseAuth.instance.currentUser!.uid)
     .collection('Notes')
     .orderBy('Time', descending: true or false).get();

This function you could pass into your Futurebuilder

  • Related