访问Firebase中的嵌套集合和文档(Flatter)
如果你不能理解标题,我很抱歉。我是NoSQL新手,不知道如何解决这个问题 我在firebase数据库中有一个嵌套集合。 在chargepoints方法中,我硬编码了(.doc('WPHW9669'),而不是硬编码,我必须在145集合中循环并首先获取文档。之后,我必须进入循环文档的内部,以访问“位置”集合 这是需要修改的代码:访问Firebase中的嵌套集合和文档(Flatter),firebase,flutter,dart,google-cloud-firestore,nosql,Firebase,Flutter,Dart,Google Cloud Firestore,Nosql,如果你不能理解标题,我很抱歉。我是NoSQL新手,不知道如何解决这个问题 我在firebase数据库中有一个嵌套集合。 在chargepoints方法中,我硬编码了(.doc('WPHW9669'),而不是硬编码,我必须在145集合中循环并首先获取文档。之后,我必须进入循环文档的内部,以访问“位置”集合 这是需要修改的代码: chargePoints() { _db .collection('bus') .doc('Routes')
chargePoints() {
_db
.collection('bus')
.doc('Routes')
.collection('145')
.doc('WPHW9669') // this the place where the first iteration have to occur
.collection('location')
.orderBy('updatedTime', descending: true)
.limit(1)
.get()
.then((docs) {
if (docs.docs.isNotEmpty) {
for (int i = 0; i < docs.docs.length; i++) {
LatLng position = LatLng(
docs.docs[i].data()['position']['geopoint'].latitude,
docs.docs[i].data()['position']['geopoint'].longitude);
print(docs.docs[i].data()['position']['geopoint'].latitude);
initMarker(position, docs.docs[i].id);
// notifyListeners();
// initMarker(docs.docs[i].data(), docs.docs[i].id);
}
}
});
}
chargePoints(){
_分贝
.collection('总线')
.doc(‘路线’)
.collection('145')
.doc('WPHW9669')//这是必须进行第一次迭代的地方
.collection('位置')
.orderBy('updateTime',降序:true)
.限额(1)
.get()
.然后((文档){
if(docs.docs.isNotEmpty){
for(int i=0;i
所以我找到了我想要的答案
我没有创建复杂的集合,而是将集合更改为简单的集合。
新集合URL-145/(总线号)/位置/
final result = FirebaseFirestore.instance.collection('145');
busPoint() {
result.get().then((snapshot) {
snapshot.docs.forEach((element) {
chargePoints(element.id.toString());
print(element.id);
});
});
}
上述方法将在“145”集合中循环,并为找到的每个总线号调用chargePoints()方法(我将总线号注册为文档id)
chargePoints(字符串元素){
_分贝
.collection('145')
.doc(元素)
.collection('位置')
.orderBy('updateTime',降序:true)
.限额(1)
.get()
.然后((文档){
if(docs.docs.isNotEmpty){
for(int i=0;i
你的问题是什么?到目前为止,您所拥有的有什么问题吗?@Doug Stevenson在我硬编码的chargepoints方法(.doc('WPHW9669')中取而代之的是,我必须遍历145个集合并首先获取文档,然后我必须进入该循环文档以访问“位置”集合请编辑问题以更清楚地了解问题所在-不要只在注释中留下信息。底部有一个编辑链接。我已经更新了@DougStevenson
chargePoints(String element) {
_db
.collection('145')
.doc(element)
.collection('location')
.orderBy('updatedTime', descending: true)
.limit(1)
.get()
.then((docs) {
if (docs.docs.isNotEmpty) {
for (int i = 0; i < docs.docs.length; i++) {
LatLng position = LatLng(
docs.docs[i].data()['position']['geopoint'].latitude,
docs.docs[i].data()['position']['geopoint'].longitude);
initMarker(position, docs.docs[i].id);
}
}
});
}