首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >只获取10个带有id的文档

只获取10个带有id的文档
EN

Stack Overflow用户
提问于 2022-02-16 12:08:37
回答 2查看 507关注 0票数 0

在这里,我只想从我的消防站获得前十名,我搜索了我如何使用我的列表视图,但我没有到任何地方。所以我想用我的证件来代替。在我的消防站里,我给出了从1到10的前10个文档it,现在我被困住了,我不知道该如何做。请帮帮忙。

代码语言:javascript
复制
static int id = 1;
StreamBuilder<QuerySnapshot>(
      stream: fireStore.collection(path).snapshots(),
      builder: (context, snapshot) {
        if(!snapshot.hasData){
          return Center(
            child: CircularProgressIndicator(
              backgroundColor: Colors.blueAccent,
            ),
          );
        }
        List<InfoBox> ancients = [];
        try {
          final collection = snapshot.data!.docs;
          for (var collect in collection) {
            final name = collect['Name'];
            final image = collect['Image'];
            final collectionWidget = InfoBox(
                ancientName: name,
                ancientImage: image
            );
            ancients.add(collectionWidget);
          }
        }catch(e){
          print('problems in stream builder \n error : $e');
        }
        return Expanded(
            child:ListView(
              children: ancients,
            )
        );
      },
    );
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-02-16 12:35:08

您可能正在搜索limit()函数。根据文档

若要限制从查询返回的文档数量,请对集合引用使用限制方法。

您可以这样实现它:

代码语言:javascript
复制
fireStore.collection(path).limit(10).snapshots();
票数 0
EN

Stack Overflow用户

发布于 2022-02-16 12:34:57

将您的ListView更改为此列表,如果一切正常,并且您正在列表中获取数据,则此操作将有效。

代码语言:javascript
复制
ListView.builder(
  itemCount: ancients.length,
  itemBuilder: (BuildContext context, int index) {

    return ListTile(
    leading: new Image.network(ancients[index].ancientImage),
  title: new Text(ancients[index].ancientName),
    )
  },
  )
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71141603

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档