Using this I use the id of the document to identify the document that I want to modify but what if want to use a value inside of that document instead? For example:
dbUserDoc.collection('shoplist').get().then((querySnapshot) => {
// ignore: avoid_function_literals_in_foreach_calls
querySnapshot.docs.forEach((result) {
final docPro = FirebaseFirestore.instance
.collection('products')
.doc(result.id); //here i use the document id but i want to use a value inside of that document
print(result.id);
docPro.get().then((DocumentSnapshot doc) {
final data = doc.data() as Map<String, dynamic>;
final int stockInt =
data['stock'] - int.parse(result['quantity']);
final stock = <String, int>{"stock": stockInt};
docPro.set(stock, SetOptions(merge: true));
});
})
});
2
Answers
The
result
object is an object of type DocumentSnapshot. If you want to display the value of a field, not the value of the document ID, which in your case are the same, then please use:This means that you’ll print the value of the
id
field that exists inside the document.If you want to show the value of the
id
field inside of theproducts
document, you can do: