skip to Main Content

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));
              });
            })
          });

example

2

Answers


  1. 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:

    print(result[id]);
    

    This means that you’ll print the value of the id field that exists inside the document.

    Login or Signup to reply.
  2. If you want to show the value of the id field inside of the products document, you can do:

    final product = result.data() as Map<String, dynamic>;
    print(product["id"]);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search