1

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);

  }
1
  • I would add Time like this:" 'Time': FieldValue.serverTimestamp()", and "DateTime mynote_time=((data['Time']?? Timestamp.now()) as Timestamp).toDate();" But I'm not sure. Commented Dec 11, 2021 at 13:11

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

by default it's false... I would say It's already ordered. It's just for reverse.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.