我是Firebase的新手,我想阅读所有的文档数据,这里是我正在尝试阅读的方式。
这是我的函数来获取数据。
Future<List<AgencyModel>> getAgencyData() async {
List<AgencyModel> agencyListModel = [];
try {
agencyListModel = await _db.collection('colelctionName')
.doc('myDoc')
.snapshots()
.map((doc)=> AgencyModel.fromJson(doc.data()!)).toList();
print('List : ${agencyListModel.length}');
return agencyListModel;
} catch (e) {
debugPrint('Exception : $e');
rethrow;
}
}这就是我如何调用上面的函数。
getAgencyDetails() async {
List<AgencyModel> data = await fireStoreService.getAgencyData();
print('Data : ${data.first}');}这是我来自this函数的模型类
factory AgencyModel.fromJson(Map<String, dynamic> json) {
return AgencyModel(
agencyName: json['agencyName'],
agencyContact: json['agencyContact'],
agencyAddress: json['agencyAddress'],
cnic: json['cnic'],
agencyContactDetails: json['agencyContactDetails'],
certificatesUrl: json['certificatesUrl'],
locationUrl: json['locationUrl'],
earning: json['earning'],
processing: json['processing'],
onHold: json['onHold']);}我没有得到任何错误或异常,这两个打印语句也不工作,甚至不显示任何东西,甚至字符串列表:和Data : i.e。
print('List : ${agencyListModel.length}');
print('Data : ${data.first}');}发布于 2022-08-23 08:31:51
更新:根据文档:https://firebase.google.com/docs/firestore/query-data/get-data#dart_1
必须区分是只检索一次数据还是实时侦听文档上的更改。
在我看来,您似乎想完成1.只想检索一次数据的情况。那样的话。你应该改变:
agencyListModel = await _db.collection('collectionName')
.doc('myDoc')
.snapshots()
agencyListModel = await _db.collection('collectionName')
.doc('myDoc')
.get()https://stackoverflow.com/questions/73455291
复制相似问题