英文:
Flutter entering to sub-sub collection in firebase
问题
我的嵌套集合看起来是这样的:
User--> drugs--> drugsTime
我想要更新子子集合(drugsTime)中文档中的数据,但我不知道药物文档的ID和药物时间文档的ID,所以我做了以下操作:
ConfirmDrugTaken(DateTime d) async {
final User? user = FirebaseAuth.instance.currentUser;
final _uid = user?.uid;
var drugID = "";
// 获取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 = await 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});
});
}
但是出现了以下错误:
[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):
E/flutter (17976): #1 _drugSetupState.setAlarm.
E/flutter (17976):
是否有另一种方法可以进入子子集合而不知道ID,或者我如何修复这个错误?
请帮助。
英文:
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
答案1
得分: 0
你忘记使用await
来调用异步方法,代码应该如下所示,针对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>>;
英文:
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>>;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论