【发布时间】:2022-01-18 13:24:30
【问题描述】:
我需要按照我显示的路径获取我的数据。
shipment collection > collection id (also this is user id) > myYuks collection > doc's id > doc's field
我有一个 YukModel 和一个数据库服务。
My YukModel codes;
class YukModel {
String? yukID;
String? yukBaslik;
String? icerik;
int? agirlik;
Timestamp? createTime;
String? aracTipi;
bool? onayDurumu;
YukModel(
{this.yukID,
this.yukBaslik,
this.icerik,
this.agirlik,
this.createTime,
this.aracTipi,
this.onayDurumu});
YukModel.fromDocumentSnapshot(DocumentSnapshot documentSnapshot) {
yukID = documentSnapshot.id;
yukBaslik = documentSnapshot.get('yukBaslik');
icerik = documentSnapshot.get('icerik');
agirlik = documentSnapshot.get('agirlik');
createTime = documentSnapshot.get('createTime');
aracTipi = documentSnapshot.get('aracTipi');
onayDurumu = documentSnapshot.get('onayDurumu');
}
}
我的数据库代码片段;
class DataBaseFB {
final FirebaseFirestore _firebaseFirestore = FirebaseFirestore.instance;
getYukListFromDB() {
_firebaseFirestore.collection("shipment").get().then((querySnapshot) async {
for (var resultA in querySnapshot.docs) {
await _firebaseFirestore
.collection("shipment")
.doc(resultA.id)
.collection("myYuks")
.get()
.then((querySnapshot) async {
for (var resultB in querySnapshot.docs) {
await _firebaseFirestore
.collection("shipment")
.doc(resultA.id)
.collection("myYuks")
.doc(resultB.id)
.get()
.then((value) {
YukModel yukModel = YukModel.fromDocumentSnapshot(value);
debugPrint("-----------------------YUKLER---------------\n"
"yukBaslik: ${yukModel.yukID}\n"
"iccerik: ${yukModel.icerik}");
});
}
});
}
});
}
}
我尝试使用主页中的按钮访问 getYukListFromDB。然后我得到了这个错误;
发生了异常。 StateError(错误状态:字段不存在 在 DocumentSnapshotPlatform 中)
我该怎么办?
【问题讨论】:
-
报错说明你要搜索的字段在数据模型中不存在。当您使用 firebase 时,您无需在 Flutter 应用程序中创建模型类,您可以直接收听实时数据库。并从数据库中获取特定数据
-
但我更喜欢分离我的所有树。因此,这些字段已经存在于 Firebase 和 YukModel 中。抱歉,您的回答并没有解决我的问题。谢谢。
标签: firebase flutter google-cloud-firestore