【问题标题】:Creating a document in cloud firestore using flutter使用 Flutter 在 Cloud Firestore 中创建文档
【发布时间】:2020-05-11 07:08:57
【问题描述】:

我在解决一个问题时遇到了很多困难。

在flutter上使用cloud_firestore,我如何在集合中创建一个新文档(如果它不存在),在该集合中创建一个新文档,并在该文档中创建一个具有名称、bool类型和类型的新字段值为真?

这就是我现在拥有的:

  await Firestore.instance
      .collection(collectionName)
      .document(documentName)
      .get()
      .then((DocumentSnapshot ds) {
         // do something here

  });

谢谢。

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore


    【解决方案1】:

    要创建新文档,需要使用add()

      Firestore.instance.collection("users").add(
      {
        "name"    : "peter",
        "boolVal" : true
      }).then((value){
        print(value.documentID);
      });
    

    add() 将创建一个具有随机 id 的文档,如果您想使用自己的 id,请使用setData()

      Firestore.instance.collection("users").document(id).setData(
      {
        "name" : "peter",
        "boolVal" : true
      }).then((value){
        print(value.documentID);
      })
    

    【讨论】:

    • 感谢您的帮助。你有文档的链接吗?我找不到用于颤振的良好参考
    • 当然!让我先测试一下,看看它是如何工作的
    猜你喜欢
    • 2020-07-02
    • 2018-11-06
    • 2018-07-11
    • 2018-10-29
    • 2020-07-08
    • 2020-06-26
    • 1970-01-01
    • 2018-11-01
    • 1970-01-01
    相关资源
    最近更新 更多