Flutter/Firebase-输入"Future< int>"不是'int'类型的子类型 [英] Flutter/Firebase - type 'Future<int>' is not a subtype of type 'int'

查看:57
本文介绍了Flutter/Firebase-输入"Future< int>"不是'int'类型的子类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手,这可能是一个简单的问题,但我正在寻找一些解释,以帮助我更多地了解期货和未来建筑商.我正在尝试查询firebase(取决于用户在上一个屏幕上单击的内容),并且我想等待所有数据加载后再显示任何内容.为此,我认为未来的构建者将是最合适的,因为我将展示一些不会带来很大变化的产品.我创建了我的getData函数:

I'm new to flutter and this is probably a simple question but i'm looking for a bit of an explanation to help me understand futures and future builder more. I am trying to query firebase (depending on what a user clicks on a previous screen) and i'd like to wait for all the data to be loaded before anything is shown. To do this I thought a future builder would be most appropriate as i'm going to be showing some products that won't change a great deal. I created my getData function:

  Future getCardData() async {
    return await Firestore.instance.collection('cards')
        .where('Event', isEqualTo: widget.documentid).snapshots();
    }
  }

并在此处实现它:

  body: FutureBuilder(
    future: getCardData(),
    builder: (context, snapshot) {
      if (!snapshot.hasData) {
        return Center(child: const Text('Loading...'));
      }
      return GridView.builder(
        shrinkWrap: true,
        gridDelegate:
        SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
        itemCount: snapshot.data.length,
        itemBuilder:
            (BuildContext context, int index) {
          print(snapshot.data.documents[index].data['img_url']);
          return Padding(
            padding: const EdgeInsets.all(8.0),
            child: Container(
              height: MediaQuery.of(context).size.height / 6,
              child: Center(
                  child: Card(
                    elevation: 8.0,
                    child: Image(
                      image: FirebaseImage(snapshot.data.documents[index].data['img_url']),
                    ),
                  )
              ),
            ),
          );
        },
      );
    },
  ),

但是我(由于我假设是GridView.buider的itemCount部分)收到一条错误消息:

But i'm getting (from what I assume to be the itemCount part of my GridView.buider) an error saying:

type 'Future<int>' is not a subtype of type 'int'

我想知道如何访问此将来值,然后调整代码的其余部分以等待数据加载,然后再显示任何图像.

I'm wondering how I can access this future value and then adjust the remainder of my code to wait for the data to load before showing any images.

推荐答案

snapshots()返回 Stream ,因此将其更改为 getDocuments():

snapshots() returns a Stream, therefore change it to getDocuments():

Future<QuerySnapshot> getCardData() async {
  return await Firestore.instance.collection('cards')
    .where('Event', isEqualTo: widget.documentid).getDocuments();
}

然后在 itemCount 中,您可以执行以下操作:

Then in the itemCount you can do the following:

itemCount: snapshot.data.documents.length,

这篇关于Flutter/Firebase-输入"Future&lt; int&gt;"不是'int'类型的子类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆