is it possible to get specific documents based on a value inside list of maps in firebase cloud store in flutter app?


im trying to make a screen in my flutter app so each user(translator) can get the appointments assigned to him/her , the appointments are inside List of Maps in the clients documents as shown in the photo Firestore document image

here is my Appointment class

class Appointment {
  String date;
  String hospital;
  String translator;
  String translatorPhone;
  String driver;
  String driverPhone;
  String description;
  String doctor;
  String procedure;


im able to get all the appointments as following

      stream: FirebaseApi.getUserAppointments(),
      builder: (context, snapshot) {
        // print(' snapshot print ===  ${}');
        switch (snapshot.connectionState) {
          case ConnectionState.waiting:
            return Center(child: CircularProgressIndicator());
            if (snapshot.hasError) {
              return Text('Something Went Wrong Try later');
            } else {
              //  final appointments =
              var documents =
                  .map((e) => e.get('appointments'))
              print('docs = ${documents}');

here is the printed result of all appointments:

docs = [[{doctor: sabri, date: 20.08.2019, driver: farukk, translator:
racha, translatorPhone: 333, description: desc123, driverPhone: 888,
procedure: surgery22, hospital: medicalpark}], [{date: 20.5.2021,
doctor: burak, driver: faruk, translator: ali, translatorPhone: 123,
description: desc, driverPhone: 3214, procedure: surgery, hospital:
medicana}, {doctor: burak222, date: 20.12.2021, driver: faruk222,
translator: meral, translatorPhone: 555, description: desc222,
driverPhone: 312, procedure: surgery222, hospital: medicana}]]

but im stuck between maps and lists while filtering the appointments according to the signed in user name

what i want to do is to show only appointments when the translator field value inside appointments array of type List<Map<String , dynamic >> is = current user

any help would be appreciated <3


documents is a of type List<List<Map<String, dynamic>>>.

So do this:

// first we flatten the documents from List<List> to List.
List flat = documents.expand((i) => i).toList();
List appointments =
    flat.where((element) => element['translator'] == currentUser).toList();
// TODO: Replace currentUser above with the user.

Or you can use this

List appointments = [];
for (var document in documents)
  for (var doc in document)
    if (doc['translator'] == currentUser) appointments.add(doc);

But this is ineffective, you have to download all appointments whenever you need to get the appointments for a user. Rather, make appointments a collection, then call it like this:

    .where('translator', isEqualTo: currentUser);

Answered By – Peter O.

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published