[英]Firestore/Flutter: Iterating through the ID of a collection of documents and output them as a list
I want to write a code that outputs the document id of each document within a document that is in a collection that the StreamBuilder
is subscribed to.我想编写一个代码,输出文档中每个文档的文档 ID,该文档位于StreamBuilder
订阅的集合中。
My Firestore structure is this:我的 Firestore 结构是这样的:
My code might seem complicated but what I'm trying to achieve in a nutshell, is to output the document ids of all documents in a collection (Scores) into a list, while the StreamBuilder
is subscribed to the changes of the document collecting (MyClass) the collection.我的代码可能看起来很复杂,但简而言之,我想要实现的是 output 将集合(分数)中所有文档的文档 ID 放入列表中,而StreamBuilder
订阅了文档收集的更改(MyClass ) 集合。 However, it gives me this error: NoSuchMethodError: 'doc', method not found, Receiver: null, Arguments:[] I think I'm having a syntax error, but I have no idea how to correct it.但是,它给了我这个错误: NoSuchMethodError: 'doc', method not found, Receiver: null, Arguments:[]我想我有一个语法错误,但我不知道如何纠正它。 I'm also not sure whether the use of the map
method is correct.我也不确定使用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(),
),
);
}
Here's the full error log (after changing to Thavamani's code):这是完整的错误日志(更改为 Thavamani 的代码后):
Your current listener schoolYear.doc(classname).snapshots()
listen to the changes made on your document MyClass
and it doesn't carries it's sub-collection Scores
details.您当前的侦听schoolYear.doc(classname).snapshots()
侦听对文档MyClass
所做的更改,并且它不携带它的子集合Scores
详细信息。
To get all document IDs inside Scores
collection, you can have either stream/future that gets all the documents available in the collection.要获取Scores
集合中的所有文档 ID,您可以使用 stream/future 来获取集合中可用的所有文档。
Ex, stream builder way例如,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()
instead of collection('Scores').snapshots()
如果您不想收听文档,只需 go 和未来的构建器使用collection('Scores').getDocuments()
而不是collection('Scores').snapshots()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.