【问题标题】:How to store maps to a firestore array如何将地图存储到 Firestore 数组
【发布时间】:2019-12-31 02:03:30
【问题描述】:

我在 abc 集合中有 xyz 文档。

  • 我想将 ma​​p 添加到 document xyz 内的 an array

我可以使用此代码添加单个数组项

CollectionReference collection = this.collection(collection);
DocumentReference ref = collection.document(document);
ref.setData({"data": FieldValue.arrayUnion(values)});

这里的data是父数组。

但是当我尝试添加另一个 ma​​p 时,firestore 替换索引 0 的值,它 不会将地图添加到下一个索引 表示索引 1,2,3 或.....

让我们看看例子 这是地图

    values = {
        "tokens": "99",
        "title": "Cleaning",
        "location": {"  lat": "34.4333", "lng": "53.4343"},
        "id": "gVnGE5ZPRnQ8HYQq7dvT",
        "client": "LFBXXj7Zi0xOr0FqBGID",
        "date": "22/8/2019",
        "message": "request message...",
        "budget": "150",
        "details": {
        "  When would you like to have the cleaners over?":
            "week",
        "What type of cleaning package would you prefer?":
            "answer for the type of clean",
        "Do you need the cleaning tools and materials?":
            "false",
        "What time do you prefer to have the cleaners over?":
            "8/20/2019"
        }
    }

我想在每次将数据发送到 Firestore 时将其添加为新的数组项

例如。

    onTap() {
        CollectionReference collection = this.collection(collection);
        DocumentReference ref = collection.document(document);
        ref.setData({"value": FieldValue.arrayUnion(values)});
    }

这里是代码

    var values = {
        "tokens": "99",
        "title": "Cleaning",
        "location": {"  lat": "34.4333", "lng": "53.4343"},
        "id": "gVnGE5ZPRnQ8HYQq7dvT",
        "client": "LFBXXj7Zi0xOr0FqBGID",
        "date": "22/8/2019",
        "message": "request message...",
        "budget": "150",
        "details": {
        "  When would you like to have the cleaners over?":
            "week",
        "What type of cleaning package would you prefer?":
            "answer for the type of clean",
        "Do you need the cleaning tools and materials?":
            "false",
        "What time do you prefer to have the cleaners over?":
            "8/20/2019"
        }
    }

    onTap() {
        CollectionReference collection = this.collection(collection);
        DocumentReference ref = collection.document(document);
        ref.setData({"value": FieldValue.arrayUnion(values)});
    }

【问题讨论】:

    标签: arrays firebase flutter google-cloud-firestore maps


    【解决方案1】:

    您正在使用覆盖现有值的setData 函数。您需要改用 updateData 函数,它将值添加到现有数组中。只需将您的 onTap 函数更改为:

    onTap() {
            CollectionReference collection = this.collection(collection);
            DocumentReference ref = collection.document(document);
            ref.updateData({"value": FieldValue.arrayUnion(values)});
    }
    

    【讨论】:

      猜你喜欢
      • 2021-10-14
      • 2021-10-20
      • 1970-01-01
      • 2018-12-31
      • 1970-01-01
      • 1970-01-01
      • 2021-03-14
      • 2021-07-23
      • 2014-03-08
      相关资源
      最近更新 更多