Home > Net >  how to query (filter) documents from firebase to flutter app based on a value nested in a List of Ma
how to query (filter) documents from firebase to flutter app based on a value nested in a List of Ma

Time:11-23

im trying to get documents from my 'Patients' Collection which contain an array of maps 'Appointments' based on the value of 'translator' key as shown here:

here is how im trying to query the documents but i guess im having a syntax error

  static Stream<QuerySnapshot> getSpecificUserAppointments(String translatorName) {
    return FirebaseFirestore.instance
        .collection('Patients')
        .where('appointments', arrayContains: translatorName)
        .snapshots();
  }

i also tried to filter this way

 .where('appointments.translator', arrayContains: translatorName), 
 .where('appointments.translator', isEqualto: translatorName)

still not getting results

any help would be appreciated

CodePudding user response:

The arrayContains operator checks whether the exact, complete item you specify exists in the array. So your check would only work if the appointments array contained an entry that has a single string as its value with translatorName.

If you want to check whether a certain name exists, add an additional array field to your document with translatorNames, where you keep just the (unique) names of the translators in the document. Then you can use that field to query:

return FirebaseFirestore.instance
    .collection('Patients')
    .where('translatorNames', arrayContains: translatorName)
    .snapshots();
  • Related