2

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.

5
  • What does "none is working" mean exactly? If you get an error message, please add it to the question. Commented May 8, 2018 at 13:48
  • It's you!!! What a coincident... In my above sample code in 2 ways I'm trying to access that collection(trips) data. But none of those 2 ways are working. Also, not getting any error & just getting empty data. Updated my post for more clarity Commented May 8, 2018 at 13:54
  • I guess I can't help. I only used Realtime Database so far. If you don't get any data, then perhaps just the query is wrong or you might need to create an index on the server to be able to filter by createdByName Commented May 8, 2018 at 13:58
  • I also feel somewhere in my query is wrong. But, can't able to fig out what's exactly in my query is wrong. Anyway, no problem. I'll try to fig. out. Commented May 8, 2018 at 14:03
  • currently snapshots is to be given as snapshots() else listen is causing error Commented Jun 23, 2018 at 3:08

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply. I've tested this but still, I'm getting no result. I think this is not the case. :(
OMG!!! Actually, the solution is .where('trip.createdByName', isEqualTo: "Suresh"). From last two days, I'm breaking my head on this small thing. Your small suggestion spark this in my mind.
0

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. :)

1 Comment

Hello, i'd like to know if is possible to add more than one where clausule, for example, where 'createdbyName' isEqualTo suresh AND/OR where date islowthan.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.