How to add document Id in the document in firestore database in flutter application

前端 未结 2 510
暖寄归人
暖寄归人 2021-01-28 21:13

I am using below code to add document in firestore collection in a flutter application. I am not getting an idea that on how to add the document id in the document. Please guide

相关标签:
2条回答
  • 2021-01-28 21:41

    DocumentReference docRef = await Firestore.instance.collection("products").add({

     'description': product.description,
     'imageUrl': product.imageUrl,
     'price': product.price,
    });
    final newProduct = Product(
     title: product.title,
     description: product.description,
     price: product.price,
     imageUrl: product.imageUrl,
     id:docRef.documentID,
    );
    
    _items.add(newProduct);
    
    0 讨论(0)
  • 2021-01-28 21:54

    Use document() with no arguments to first generate a reference to a document with a random ID, then use setData() to create it and add the documentID as part of the new document:

    DocumentReference ref = Firestore.instance.collection("posts1").document();
    ref.setData({
        "post_id": ref.documentID,
        // ... add more fields here
    })
    
    0 讨论(0)
提交回复
热议问题