将Map或Array的数据类型上载到Firestore数据库以匹配Firestore映射和数组类型

时间:2019-01-02 04:27:24

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

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

我尝试将toString方法用于map类并将其分配给字段,但是它显示为长字符串而不是map,请参见下面的代码段

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

以上代码段将myMap的to字符串值作为字符串上传,并将其分配给mapdata。它不会将地图格式的数据发送到Firestore

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

2 个答案:

答案 0 :(得分:1)

我认为这会起作用

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

答案 1 :(得分:0)

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