如何将地图存储到 Firestore 数组

Posted

技术标签:

【中文标题】如何将地图存储到 Firestore 数组【英文标题】:How to store maps to a firestore array 【发布时间】: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));
    

【问题讨论】:

【参考方案1】:

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

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

【讨论】:

以上是关于如何将地图存储到 Firestore 数组的主要内容,如果未能解决你的问题,请参考以下文章

将地图添加到 Firestore Android 上的数组

无法访问 Firestore 快照中的数据以将标记添加到存储在 Firebase 中的 Google 地图

如何使用 Firestore 函数更新地图中的数组?

如何将值添加到从颤振到 Firestore 的数组中

在 Flutter 中将 Firestore DocumentSnapshot 转换为 Map

如何在不重新加载页面的情况下更新从 firestore 检索到的标记在我的地图上?