Home > Net >  flutter/firebase/dart: get data from firestore
flutter/firebase/dart: get data from firestore

Time:10-21

How to get docs id from some matching condition?

I was able to get this to work:

Future getChat({required String userIdsArr}) async {
    var docId = '';

    await chat.where('User ids', isEqualTo: userIdsArr).get().then((value) {
      value.docs.forEach((element) {
        docId = element.id;
      });
    });

    //print(docId);
    return docId
  }

this returns the correct record, however, I think this is a terrible way of quering the database because I have to fetch all the records everytime. Is there a way to write this so that I get the doc Id of the matching condition?

CodePudding user response:

Unfortunately, there is not a better way to accomplish this. When you use the where clause though, it won't fetch everything like you suspect, only records that contain the value you are querying for. I don't believe it's as expensive of a call as you might think.

  • Related