flutter - 列表包含“未来”的实例'
问题描述
我有这段代码尝试使用 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 打印:['Future',3esdWesqrTD_123_3dssds3,sQWdsda23dsad_da21 的实例]
谁能告诉我为什么我的列表中有这个“未来”实例以及如何避免它?
解决方案
我没有尝试运行您的代码,但我怀疑问题始于语句
return ids1.add(geoRef.within(center: center, radius: 50, field: 'position', strictMode: true).first.then((value) {
如您所见,您正在添加一些ids1.add(...)
看起来需要await
:的东西geoRef.within(...).first.(...)
。ids1.add(...)
我在 return 语句中找不到所需的调用。尝试删除它,看看它是怎么回事。
推荐阅读
- java - 为什么我的回文检查器总是评估为真?我尝试调试,结果不一样
- spring-boot - Spring boot logbcak to file在tomcat上不起作用
- amazon-web-services - 如何从 EC2 连接到本地 VPN?
- sycl - 为什么默认情况下每个 SYCL 设备都在单独的上下文中?
- mongodb - MongoDb 聚合 $count 显示 0
- javascript - 如何维护多个数组索引列表,其值由输出结果数组的函数使用?
- r - 具有多个组的 Power BI 相关性度量
- webview - Flutter WebView 插件无法播放某些 YouTube 视频
- java - 为什么将局部变量和易失性都用于 DCL(如 Sonar 工具所建议的那样)?
- c++ - 如何在 C++ 代码中使用 gnu diff 的输出?