【问题标题】:List contains Instance of 'Future<dynamic>'列表包含 'Future<dynamic>' 的实例
【发布时间】: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


    【解决方案1】:

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

    return ids1.add(geoRef.within(center: center, radius: 50, field: 'position', strictMode: true).first.then((value) {
    

    如您所见,您正在向awaitgeoRef.within(...).first.(...) 添加一些看起来需要的ids1.add(...)。我在 return 语句中找不到所需的 ids1.add(...) 调用。尝试删除它,看看效果如何。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-08
      • 1970-01-01
      • 2015-04-05
      • 2021-04-06
      • 2021-10-05
      • 2020-09-03
      • 1970-01-01
      • 2021-08-22
      相关资源
      最近更新 更多