I've Appointments collections with student and teacher as reference type along with other fields.
I want to get the list of Appointments using CollectionReference.withConverter. But because of reference fields, I'm not able to fetch them.
Example code:
FirebaseFirestore.instance.collection('appts').withConverter<Appointment>(
fromFirestore: (snapshot, _) {
Map<String, dynamic> appt = snapshot.data()!;
Student student = await appt['student'] // <---- Error as await is NOT allowed here
.get()
.then((studentSnapshot) => Student.fromMap(studentSnapshot.data()!));
return Appointment.fromMap(
snapshot.data()!
..['id'] = snapshot.id
..['student'] = student,
);
},
toFirestore: (appointment, _) => ...,
);
await is not allowed as withConverter<Appointment> expects fromFirestore to return Appointment object instead of Future<Appointment>
Without await, I'll get Future<Student>, and not sure how to map the complete document to Student type.
Is there anyway Flutter's Firestore auto-fetches the reference documents instead of I fetching them manually?