flutter - 列表包含 'Future<dynamic>' 的实例

标签 flutter dart google-cloud-firestore

我有这段代码试图使用 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 中是否有一些相关数据,但我不确定这是我应该做的,有些事情我不知道不知如何躲避;当我打印列表时,我得到以下结果:

setting state to ids ids print: [Instance of 'Future', 3esdWesqrTD_123_3dssds3, sQWdsda23dsad_da21]

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

最佳答案

我没有尝试运行你的代码,但我怀疑问题是从语句开始的

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

如您所见,您正在添加一些看起来需要awaitids1.add(...):geoRef.within(。 ..).首先.(...).我在返回语句中找不到所需的 ids1.add(...) 调用。尝试删除它,看看效果如何。

关于flutter - 列表包含 'Future<dynamic>' 的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57591033/

相关文章:

android - 未找到 : 'dart:html' import 'dart:html' ; I don't need dart:html and I also didn't used, 但我尝试通过导入它,但错误不会发生

dart - bwu_datagrid - 如何禁用对列中某些单元格的编辑器调用?

android - Flutter 大 apk 大小并针对不同的 ABI 发布

flutter - 如何在 Flutter 中 move 屏幕中的小部件

flutter - 如何根据bool值在Auth屏幕和Home屏幕之间切换?

flutter - 如何在两个小部件之间共享公共(public)参数?

node.js - 将 Firestore 依赖项和类型导入到 node.js

firebase - 如何使用 flutter cloud_firestore 包传递 firestore 身份验证 token

firebase - 用于列表构建的Flutter中的Firestore嵌套查询

dart - sqflite中的多个表