Mario Niemand
Mario Niemand

Reputation: 93

How to set documentID in firestore using flutter

I am trying to set my documentID manually in my flutter application but it keeps creating a new field inside my document named DocumentID. How do I set my documented?

  final barcode = TextEditingController();
  final price = TextEditingController();
  final stock = TextEditingController();
  final color = TextEditingController();
  ///the above are textformfield controllers I use for the data input
  addData() {
    Map<String, dynamic> theData = {
      //"DocumentID" : barcode.text,
      "Color" : color.text,
      "Price" : price.text,
      "Stock" : stock.text,
    };
    CollectionReference collectionReference = Firestore.instance.collection("products");
    collectionReference.add(theData);
  }
  addID(){
    DocumentReference dataID = Firestore.instance.document("products");
    dataID.set(DocumentReference, barcode.text);
  }

The data also displays only "" in my database when using the method addData: enter image description here

Below is mu adjusted code:

  final barcode = TextEditingController();
  final price = TextEditingController();
  final stock = TextEditingController();
  final color = TextEditingController();
  addData() {
    Map<String, dynamic> theData = {
      "Color" : color.text,
      "Price" : price.text,
      "Stock" : stock.text,
    };
    CollectionReference collectionReference = Firestore.instance.collection("products");
    FirebaseFirestore.instance.collection("products").doc(barcode.text).set(theData);
    collectionReference.add(theData);

  }

I get this error: enter image description here

Upvotes: 0

Views: 75

Answers (1)

Yadu
Yadu

Reputation: 3305

you can set it like this

        FirebaseFirestore.instance.collection("products").doc("my_own_document_id").set(data);

you can set the document id while creating it, but you cannot change it

Upvotes: 1

Related Questions