Firestore compound query not working for Flutter Web

Viewed 352

Hi I have a simple query for Firestore in a StreamBuilder

StreamBuilder(
          stream: FirestoreManager.firebaseFirestore
              .collection("orders")
              .orderBy('logs.0', descending: true)
              .where('status', whereIn: current['id'])
              .snapshots(),
          builder: (BuildContext context, AsyncSnapshot<dynamic> snap) {
            print(snap.data.toString());
            if (!snap.hasError && snap.hasData) {
              QuerySnapshot snapshot = snap.data;
              if (snapshot.documents.isNotEmpty) {
                List<DocumentSnapshot> snapList = snapshot.documents;
                return ListView.builder(
                  padding: EdgeInsets.only(right: 10, left: 10),
                  physics: ScrollPhysics(),
                  itemCount: snapList.length,
                  shrinkWrap: true,
                  itemBuilder: (BuildContext context, int index) {
                    return OrderListItem(
                      order: Order.fromJson(snapList[index].data),
                    );
                  },
                );
              } else {
                return Center(
                  child: Text(
                    "No ${current['status'].toString().trim()} Order Available...!",
                    style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
                  ),
                );
              }
            } else {
              return Center(
                child: CircularProgressIndicator(),
              );
            }
          })

It works absolutely fine for mobile appilcaiton but when I try to run it for web, It doesn't work. Actually it shows data once for a second and again disappears. I got following log in console by printing snapshot data using print(snap.data.toString());

js_primitives.dart:30 null
js_primitives.dart:30 Instance of 'QuerySnapshot'
js_primitives.dart:30 null

Why is this happening? Why it shows data once and again disappear it? If I remove either .orderBy('logs.0', descending: true) or .where('status', whereIn: current['id']) then it works fine.

1 Answers

I am having the same problem with flutter web. I am trying to use two chained where() methods and it doesn't give me any results:

StreamBuilder(
    stream: fire
        .collection('thiHistory')
        .where("device", "==", _device)
        .where("ts", '>=', ts)
        .onSnapshot,
    builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
      if (!snapshot.hasData) return Text('Loading....');

      final docs = snapshot.data.docs;

      return ListView.builder(
          scrollDirection: Axis.horizontal,
          itemCount: docs.length,
          itemBuilder: (context, index) {
            final doc = docs[index].data();
            return doc["device"] == _device ? Column(
              children: [
                Text(doc['device']),
                Text("${doc["hour"]}"),
                Text("${doc["temp"]}"),
                Text("${doc["humi"]}"),
                Text("${doc["thi"]}"),
              ],
            ) : Container();
          });
    },
  )

If I only use one where() or the other it works fine. If I use both, I always get "Loading....", which tells me the snapshot has no data.

I also tried chaining orderedBy() with where() like Shahzad Akram, and same thing... Using one or the other works great, combining them does not return data.

I am using the firebase 7.3.0 package and version 7.19.1 of the libraries:

  <script src="https://www.gstatic.com/firebasejs/7.19.1/firebase-app.js"></script>
  <script src="https://www.gstatic.com/firebasejs/7.19.1/firebase-database.js"></script>
  <script src="https://www.gstatic.com/firebasejs/7.19.1/firebase-firestore.js"></script>

Thanks for your help.

Related