【发布时间】:2019-08-21 11:53:44
【问题描述】:
我有这段代码尝试使用 GeoFlutterFire 从 Firestore 查询一些数据,但为了提高效率,我只想获取一次 id 列表,然后使用它从 Firebase 数据库中获取其余数据。 这是有问题的代码:
class _StartScreenState extends State<StartScreen> {
List ids = ['abc'];
@override
void initState() {
super.initState();
print('ids ${ids}');
}
_initState() async {
Firestore _firestore = Firestore.instance;
List ids1 = [];
Geoflutterfire geo = Geoflutterfire();
var location = new Location();
var pos = await location.getLocation();
GeoFirePoint center = geo.point(latitude: pos.latitude.toDouble(), longitude: pos.longitude.toDouble());
var collectionReference = _firestore.collection('locations');
var geoRef = geo.collection(collectionRef: collectionReference);
return ids1.add(geoRef.within(center: center, radius: 50, field: 'position', strictMode: true).first.then((value) {
value.map((doc) {
if (doc.data.isNotEmpty) {
print('doooc id here ${doc['id']}');
ids1.add(doc['id']);
print(ids1);
}
}).toList();
setState(() {
ids = ids1;
print('setting state to ids ids print: ${ids.}');
});
}));
}
就像我说的那样,我的意图是获取 ID,一旦我有列表检查 Firebase 中是否有一些相关数据,但我不确定这是我应该这样做的方式,而且有些事情我不知道不知道如何避免;当我打印列表时,我得到以下结果:
将状态设置为 ids ids print: ['Future' 实例, 3esdWesqrTD_123_3dssds3, sQWdsda23dsad_da21]
谁能告诉我为什么我的列表中有这个“未来”实例以及如何避免它?
【问题讨论】:
标签: flutter dart google-cloud-firestore