Firebase Firestore快照中的查询特定字段
我创建了这个流来获取特定文档的快照。在文档中有一个名为tripAttractions的数组,我想将它构建到一个列表中。问题是,如何从快照访问此特定阵列Firebase Firestore快照中的查询特定字段,firebase,flutter,google-cloud-firestore,Firebase,Flutter,Google Cloud Firestore,我创建了这个流来获取特定文档的快照。在文档中有一个名为tripAttractions的数组,我想将它构建到一个列表中。问题是,如何从快照访问此特定阵列 Stream<QuerySnapshot> getAttractions(BuildContext context) async* { Firestore.instance .collection('trips') .document(documentId) .snapshots(includ
Stream<QuerySnapshot> getAttractions(BuildContext context) async* {
Firestore.instance
.collection('trips')
.document(documentId)
.snapshots(includeMetadataChanges: true);
}
firestore文档中的数组
您很可能只是缺少项生成器中的数组访问器:
ListView.builder(
itemCount: snapshot.data['tripAttractions'].length,
itemBuilder: (BuildContext context, int index) =>
tripAttractionsCards(
context, index, snapshot.data['tripAttractions'][index]),
);
好的,问题是被错误地称为QuerySnapshot。我应该使用DocumentSnapshot,因为我直接调用文档。我还必须添加yield*以从firestore返回数据
Stream<DocumentSnapshot> getAttractions(BuildContext context) async* {
yield* Firestore.instance
.collection('trips')
.document(documentId)
.snapshots(includeMetadataChanges: true);
}
嘿,弗兰克!我尝试执行此操作,但出现以下错误:NoSuchMethodError:对null调用了方法“[]”。接收者:null,尝试调用:[]TripAttractionsInTesting:您可能希望检查snapshot.data为您做了什么,因为您似乎试图从空的或不存在的快照获取数据。我刚刚创建了一个if语句进行检查,快照查询没有返回任何数据。谢谢我解决了问题!我会把我做错的事发出去的谢谢你,弗兰克!第二次你用我的项目救了我。
Stream<DocumentSnapshot> getAttractions(BuildContext context) async* {
yield* Firestore.instance
.collection('trips')
.document(documentId)
.snapshots(includeMetadataChanges: true);
}
if (!snapshot.hasData) return Text("Loading");