my nested collections looks like this:
User--> drugs--> drugsTime
I want to update data in document in the subsub-collection (drugsTime) however I dont know the drug document id and the drugtime document id so I did the following :
ConfirmDrugTaken(DateTime d) async {
final User? user = FirebaseAuth.instance.currentUser;
final _uid = user?.uid;
var drugID="";
// Get the user's sub-collections in Firestore
final QuerySnapshot<Map<String, dynamic>> querySnapshot =
await FirebaseFirestore.instance
.collection("users")
.doc(_uid)
.collection('drug').where("drugName", isEqualTo: widget.title)
.get();
querySnapshot.docs.forEach((element) async {
drugID=element.id;
});
final QuerySnapshot<Map<String, dynamic>> querySnapshot2 =FirebaseFirestore.instance
.collection("users")
.doc(_uid)
.collection("drug")
.doc(drugID).collection("drugDates").where('time',isEqualTo: d).get() as QuerySnapshot<Map<String, dynamic>>;
querySnapshot2.docs.forEach((element) async {
FirebaseFirestore.instance
.collection("users")
.doc(_uid)
.collection("drug")
.doc(drugID).collection("drugDates").doc(element.id).update( {'isTaken': true});
});
}
but this error occur:
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'Future<QuerySnapshot<Map<String, dynamic>>>' is not a subtype of type 'QuerySnapshot<Map<String, dynamic>>' in type cast
E/flutter (17976): #0 _drugSetupState.ConfirmDrugTaken (package:taafi_application_v1/pages/drug_setup.dart:215:79)
E/flutter (17976): <asynchronous suspension>
E/flutter (17976): #1 _drugSetupState.setAlarm.<anonymous closure>.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:taafi_application_v1/pages/drug_setup.dart:112:20)
E/flutter (17976): <asynchronous suspension>
Is there is another way to enter to the sub-sub-collection with out knowing the id OR how can I fix this error?
please help
You forgot to use await
to call async method, code should be as follows for querySnapshot2
final QuerySnapshot<Map<String, dynamic>> querySnapshot2 = (await FirebaseFirestore.instance
.collection("users")
.doc(_uid)
.collection("drug")
.doc(drugID).collection("drugDates").where('time',isEqualTo: d).get()) as QuerySnapshot<Map<String, dynamic>>;