I can't use orderBy query from firestore if where is used Flutter

Viewed 383

I am retreiving a list from Firestore and I need to use query where to retrieve specific UserID but at the same time I need also to order this list by the timestamp. Using orderBy and where is not possible to get this is my code:

Container(
      height: MediaQuery.of(context).size.height * 0.275,
      child: StreamBuilder(
          stream: FirebaseFirestore.instance
              .collection('ConfirmedCart').orderBy('cartDate',descending: false)
              .where('idConsultant', isEqualTo: firebaseUser.uid)
              .snapshots(),
          builder: (context, snapshotConfirmedCart) {
            if (!snapshotConfirmedCart.hasData)
              return Container(
                  width: 40,
                  child: Center(
                    child: CircularProgressIndicator(
                      backgroundColor: Colors.orange,
                    ),
                  ));

as you can see above orderBy is used and can't fetch data, If I removed orderBy, I get the data but without my specific order.

1 Answers
FirebaseFirestore.instance
              .collection('ConfirmedCart')
              .where('idConsultant', isEqualTo: firebaseUser.uid)
              .orderBy('cartDate',descending: false)

When performing compound queries you must create a composite index in the firebase console. When the above query is executed you will get a link in the log that will take you to the console to create the index.

Related