Home > Software engineering >  Flutter, How to use firebase query .orderBy() on timestamp data?
Flutter, How to use firebase query .orderBy() on timestamp data?

Time:06-09

I have stored time stamp data in firebase, I am fetching it using streams and displaying it in the latest date order in a datatable widget. I used .orderBy to access descending bool. But neither true nor false worked, instead !snapshot.hasData condition is getting executed, Why orderBy is not working and what are the other queries that I can use to get the latest date order.

stream: FirebaseFirestore.instance
                        .collection('lender')
                        .doc(auth.currentUser!.email)
                        .collection('paymentData')
                        .where('name',
                            isEqualTo: Provider.of<UpdateNameProvider>(context,
                                    listen: false)
                                .bname).orderBy('paidDate', descending: true)
                        .snapshots(),

                    //.snapshots(),

                    builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
                      if (!snapshot.hasData) {
                        return const Center(
                            child: CircularProgressIndicator(
                          backgroundColor: Color(0xff8eacbb),
                        ));
                      }

CodePudding user response:

There are many possible reasons why a snapshot doesn't have data. At the very least you'll want to check if there's an error, and if so: log it somewhere.

Something like this:

builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
  if (snapshot.hasError) { //            
  • Related