Suresh
Suresh

Reputation: 5987

Flutter firestore plugin can't able to fetch data with combination of where and isEqualTo

I have a collection by name "trips" in Firestore. The data format is something like this

enter image description here

I'm trying to access documents of that Collection by using below code.

First way

try {
  Firestore.instance
      .collection("trips")
      .where("createdByName", isEqualTo: "Suresh")
      .snapshots
      .listen((data) => print('Firestore response1: ${data.documents}'));
} catch (e){
  print('Caught Firestore exception1');
  print(e);
}

Second way

try {
  Firestore.instance.collection("trips").where("createdByName", isEqualTo: "Suresh").getDocuments().then((data) {
    print('Firestore response2: , ${data.documents}');
  });
} catch (e){
  print('Caught Firestore exception2');
  print(e);
}

But, none of the above two ways is working. Don't know what I'm doing wrong.


One thing I've noticed whenever I'm removing the where("createdByName", isEqualTo: "Suresh") part from the query it's working fine. But, the moment I include that part query is returning an empty result.

Upvotes: 2

Views: 1198

Answers (2)

Suresh
Suresh

Reputation: 5987

Ok, after two days of breaking my brain for this silly thing... this is the answer to the issue... .where('trip.createdByName', isEqualTo: "Suresh")

And, the full queries are like this...

First way

Firestore.instance.collection('trips')
  .where('trip.createdByName', isEqualTo: "Suresh")
  .snapshots.listen(
      (data){
        data.documents.forEach((doc) => print( 'Firestore response1 : ${doc.data.toString()}' ));
      }
  );

Second way

Firestore.instance.collection("trips")
  .where("trip.createdByName", isEqualTo: "Suresh")
  .getDocuments().then((string) {
    string.documents.forEach((doc) => print("Firestore response2: ${doc.data.toString()}" ));
  });

Hope, this will be a help. :)

Upvotes: 0

Luke
Luke

Reputation: 6778

I'm not sure if the screenshot is accurate but it looks like you have a trip node as well. That would put the hierarchy as trips.trip.createdByName, no? If so, it's likely that the where clause is wrong.

Upvotes: 1

Related Questions