【问题标题】:flutter-firestore: how to retrieve map field and array fieldflutter-firestore:如何检索地图字段和数组字段
【发布时间】:2021-06-13 08:50:11
【问题描述】:

以下是我在firestore中保存用户的geohashgeopoint的方式。

我的问题是我不知道如何从 firestore 检索 g.geohashg.geopoint

比如我想读取字段intro,我这样写代码;

FirebaseFirestore.instance.collection('...').doc(..).data()['intro']

但我不知道如何从 Firestore 中检索 g.geohashg.geopointg.geohash 在地图内,g.geopoint 是地图内的数组。如何从我的 Firestore 中检索像 g.geohash 和 array-inside-a-map 字段 g.geopoint 这样的地图字段?

【问题讨论】:

    标签: arrays flutter dictionary google-cloud-firestore get


    【解决方案1】:

    无法读取文档中的字段子集。 您必须获取整个文档,然后使用点表示法或 FieldPath 来获取嵌套数据:

        FirebaseFirestore.instance
        .collection('users')
        .doc(userId)
        .get()
        .then((DocumentSnapshot documentSnapshot) {
          if (documentSnapshot.exists) {
            try {
              dynamic nested = snapshot.get(FieldPath(['g', 'geopoint']));
              } on StateError catch(e) {
                print('No nested field exists!');
              }
          } else {
            print('Document does not exist on the database');
          }
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-09
      • 2022-01-18
      • 1970-01-01
      • 2020-07-12
      • 1970-01-01
      • 2018-03-26
      • 2021-07-20
      相关资源
      最近更新 更多