Why does flutter streambuilder query using 'orderby' cause an error

Issue

Why does my StreamBuilder return something went wrong? This stream works perfectly fine but when add orderBy it says that something went wrong.
Database

Padding(
                      padding: const EdgeInsets.only(
                        left: 20,
                      ),
                      child: StreamBuilder<QuerySnapshot>(
                        stream: FirebaseFirestore.instance
                            .collection("notifications")
                            .where("parentUID",
                                isEqualTo:
                                    FirebaseAuth.instance.currentUser!.uid)
                            .orderBy('timestamp')
                            .snapshots(),
                        builder: (BuildContext context,
                            AsyncSnapshot<QuerySnapshot> snapshot) {
                          if (snapshot.hasError) {
                            return Text('Something went wrong');
                          }

                          if (snapshot.connectionState ==
                              ConnectionState.waiting) {
                            return Text("Loading");
                          }
                          return Column(
                            children: snapshot.data!.docs.map(
                              (DocumentSnapshot document) {
                                Map<String, dynamic> data =
                                    document.data()! as Map<String, dynamic>;
                                return adminNotifCard(
                                  text1: data['notifTitle'],
                                  text2: data['notifNotes'],
                                );
                              },
                            ).toList(),
                          );
                        },
                      ),
                    ),

Error

Solution

I solved the issue by going to the index tab in cloud firestore
enter image description here

enter image description here

And chose the notification collection and made it ascending, it didnt work at first, but after saying building, it said enabled and I hot restart. It worked

Answered By – Mohammad Abd-Elmoniem

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published