Solution 1 :
snapshot.data.forEach((doc) here doc is of type DocumentSnapshot you cannot assign it in courseRef.document(doc) as it takes a string .Instead you should use courseRef.document(doc.documentId)
Here is the updated code.
return StreamBuilder(
stream: courseRef.snapshots(),
builder: (context, AsyncSnapshot<QuerySnapshot> snapshot){
if (!snapshot.hasData){
return circularProgress();
}
snapshot.data.forEach((DocumentSnapshot doc){
courseRef.document(doc.documentId).updateData({
'id' : doc.id,
'isTutor': '',
'isStudent': '',
});
});
});
}
Problem :
I’m trying to fetch all the documents in a firestore collection and add fields in those documents in flutter but I’m not sure how to go about it. I tried this but it didn’t seem to do anything.
return StreamBuilder(
stream: courseRef.snapshots(),
builder: (context, snapshot){
if (!snapshot.hasData){
return circularProgress();
}
snapshot.data.forEach((doc){
courseRef.document(doc).updateData({
'id' : doc.id,
'isTutor': '',
'isStudent': '',
});
});
});
}
Comments
Comment posted by Priscilla Abhulimen
I have no idea why this was downvoted when I need assistance.
Comment posted by Shubham Gupta
snapshot.data.forEach((doc)
Comment posted by Frank van Puffelen
Good catch @ShubhamGupta. Do you want to write it up as an answer, so that Priscilla can accept it and others can upvote it?
Comment posted by Priscilla Abhulimen
@ShubhamGupta you should
Comment posted by Shubham Gupta
I have posted it as an answer and I would strongly recommend you define the type that will save you from such errors.