![](/img/trans.png)
[英]Loop through all firestore documents in a collection AND update them in flutter
[英]Firestore/Flutter: Iterating through the ID of a collection of documents and output them as a list
我想编写一个代码,输出文档中每个文档的文档 ID,该文档位于StreamBuilder
订阅的集合中。
我的 Firestore 结构是这样的:
我的代码可能看起来很复杂,但简而言之,我想要实现的是 output 将集合(分数)中所有文档的文档 ID 放入列表中,而StreamBuilder
订阅了文档收集的更改(MyClass ) 集合。 但是,它给了我这个错误: NoSuchMethodError: 'doc', method not found, Receiver: null, Arguments:[]我想我有一个语法错误,但我不知道如何纠正它。 我也不确定使用map
方法是否正确。
classname = "MyClass";
CollectionReference schoolYear =
FirebaseFirestore.instance.collection("2020-2021");
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.all(30),
child: StreamBuilder(
builder: (context, streamSnapshot) {
return SingleChildScrollView(
child: DataTable(
columns: streamSnapshot.data.doc.map((DocumentSnapshot document) {
return new DataColumn(label: Text(document.id));
}).toList(),
rows: <DataRow>[],
),
);
},
stream: schoolYear.doc(classname).snapshots(),
),
);
}
您当前的侦听schoolYear.doc(classname).snapshots()
侦听对文档MyClass
所做的更改,并且它不携带它的子集合Scores
详细信息。
要获取Scores
集合中的所有文档 ID,您可以使用 stream/future 来获取集合中可用的所有文档。
例如,stream 构建器方式
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.all(30),
child: StreamBuilder(
builder: (context, streamSnapshot) {
if (streamSnapshot.hasError) {
// print() you could check errors here
return Icon(
Icons.error_outline,
color: Colors.red,
size: 60,
);
} else if (streamSnapshot.hasData) {
return SingleChildScrollView(
child: DataTable(
columns: streamSnapshot.data.docs.map((DocumentSnapshot document) {
return new DataColumn(label: Text(document.documentID));
}).toList(),
rows: <DataRow>[],
),
);
} else {
return SizedBox(
child: const CircularProgressIndicator(),
width: 60,
height: 60,
);
}
stream: schoolYear.doc(classname).collection('Scores').snapshots(),
),
);
}
collection('Scores').getDocuments()
而不是collection('Scores').snapshots()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.