Issue
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;
Appointment(
{this.hospital,
this.doctor,
this.date,
this.description,
this.driver,
this.translator,
this.translatorPhone,
this.driverPhone,
this.procedure});
im able to get all the appointments as following
StreamBuilder<QuerySnapshot>(
stream: FirebaseApi.getUserAppointments(),
builder: (context, snapshot) {
// print(' snapshot print === ${snapshot.data.docs}');
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return Center(child: CircularProgressIndicator());
default:
if (snapshot.hasError) {
return Text('Something Went Wrong Try later');
} else {
// final appointments =
var documents = snapshot.data.docs
.map((e) => e.get('appointments'))
.toList();
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
Solution
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();
print(appointments);
// 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);
print(appointments);
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:
FirebaseFirestore.instance
.collection('appointments')
.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