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.
My Firestore structure is this:
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. 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. I'm also not sure whether the use of the map
method is correct.
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):
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.
To get all document IDs inside Scores
collection, you can have either stream/future that gets all the documents available in the collection.
Ex, stream builder way
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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.