【问题标题】:Uploading data type of Map or Array to Firestore database to match Firestore map and array type将 Map 或 Array 的数据类型上传到 Firestore 数据库以匹配 Firestore 地图和数组类型
【发布时间】:2019-05-28 19:07:57
【问题描述】:

我有一个 Map<String, String> 类型的地图,我正在尝试将其上传到 Firestore 以匹配作为字段可用的地图数据类型。见下图。

我已经尝试使用地图类的 toString 方法并将其分配给一个字段,但它显示为一个长字符串而不是地图,请参见下面的代码 sn-p

Firestore.instance.collection('/mycollection').add({
  'mapData' : myMap.toString(), // myMap.toString() = {data: test, data2: name}
}).then((onValue) {
  // do something
}).catchError((e) {
  print(e);
});

上面的sn-p将myMap的to字符串值作为字符串上传并赋值给mapdata。它不会将地图格式的数据发送到 Firestore

关于如何将 Map<string, string> 类型发送到 Firestore 以映射 Firestore 提供的 map 类型的任何想法?还有从 List 到 Firestore array

【问题讨论】:

  • 我认为您已经尝试将myMap 直接用作mapData 的字段值,但没有达到您的预期?
  • 不,我实际上并没有,我只是做了,而且效果很好。

标签: firebase dart flutter google-cloud-firestore firebase-storage


【解决方案1】:

将 Map 或 Array 的数据类型上传到 Firestore 数据库以匹配 Firestore 地图和数组类型 回答: 使用 Angular

    rate: any = [];

    constructor(
        public db: AngularFirestore,
      ) {}



   addnew() {
     try {
        this.db.collection('try').add({ 
           sharedWith: [{ who: "me", when: new Date() }]
          })
      }
      catch (error) {
        console.log('Error', error);
      }
    }

**Add Map data Type To firestore**


addnew() {
     try {
        this.db.collection('try').add({
           sharedWith: { who: "me", when: new Date() }
        })
      }
      catch (error) {
        console.log('Error', error);
      }
    }

【讨论】:

  • 我没有看到地图对象
【解决方案2】:

我认为这会奏效

Firestore.instance.collection('/mycollection').add({
  'mapData' : myMap,
}).then((onValue) {
  // do something
}).catchError((e) {
  print(e);
});

【讨论】:

  • 这可能不会创建对象类型的字段,如屏幕截图所示。屏幕截图中的文档有一个字段thisMap,它包含一个具有三个属性的对象。
  • @DougStevenson 你是对的。错过了一个小细节。让我重新考虑
  • 你说得对,这确实有效,谢谢,它以地图的形式发送到 Firestore
猜你喜欢
  • 2020-04-28
  • 2019-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-03
  • 2021-03-23
相关资源
最近更新 更多