flutter-firestore:如何检索映射字段和数组字段
flutter-firestore: how to retrieve map field and array field
以下是我在firestore中保存用户geohash
和geopoint
的方法
我的问题是我不知道如何从 firestore 中检索 g.geohash
或 g.geopoint
。
比如我要读取字段intro
,我这样写代码;
FirebaseFirestore.instance.collection('...').doc(..).data()['intro']
但我不知道如何从 firestore 中检索 g.geohash
和 g.geopoint
。 g.geohash
在地图内部,g.geopoint
是地图内部的数组。如何从我的 firestore 中检索像 g.geohash
和 array-inside-a-map 字段 g.geopoint
这样的地图字段?
无法读取文档中字段的子集。
您必须获取整个文档,然后使用点符号或 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');
}
});
以下是我在firestore中保存用户geohash
和geopoint
的方法
我的问题是我不知道如何从 firestore 中检索 g.geohash
或 g.geopoint
。
比如我要读取字段intro
,我这样写代码;
FirebaseFirestore.instance.collection('...').doc(..).data()['intro']
但我不知道如何从 firestore 中检索 g.geohash
和 g.geopoint
。 g.geohash
在地图内部,g.geopoint
是地图内部的数组。如何从我的 firestore 中检索像 g.geohash
和 array-inside-a-map 字段 g.geopoint
这样的地图字段?
无法读取文档中字段的子集。 您必须获取整个文档,然后使用点符号或 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');
}
});