首页 > 解决方案 > 列表包含“未来”的实例'

问题描述

我有这段代码尝试使用 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 的实例]

谁能告诉我为什么我的列表中有这个“未来”实例以及如何避免它?

标签: flutterdartgoogle-cloud-firestore

解决方案


我没有尝试运行您的代码,但我怀疑问题始于语句

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 语句中找不到所需的调用。尝试删除它,看看它是怎么回事。


推荐阅读