本文介绍了如何使用Flutter在Firestore中设置documentID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在flutter应用程序中手动设置我的documentID,但它会在我的文档中继续创建一个名为DocumentID的新字段.如何设置文件记录?

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

数据也仅显示".在我的数据库中使用方法addData时:

The data also displays only "" in my database when using the method addData:

下面是mu调整后的代码:

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

  }

我收到此错误:

推荐答案

您可以这样设置

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

您可以在创建文档时设置文档ID,但不能更改

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

这篇关于如何使用Flutter在Firestore中设置documentID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 05:56