如何在颤动中为 listview.builder 制作列表视图?

Posted

技术标签:

【中文标题】如何在颤动中为 listview.builder 制作列表视图?【英文标题】:How to make list view to listview.builder in flutter? 【发布时间】:2020-09-25 12:59:04 【问题描述】:
buildComments() 

return StreamBuilder(

    stream: commentRef
        .document(postId)
        .collection('comments')
        .orderBy('timestamp', descending: false)
        .snapshots(),

    builder: (context, snapshot) 
      if (!snapshot.hasData) 
        return circularProgress();
      

      List<Comment> comments = [];
      snapshot.data.documents.forEach((doc) 
        print(comments);
        comments.add(Comment.fromDocument(doc));
      );

      return ListView(
        children: comments,
      );
    );

我试图在 list view.builder 中转换它,但它给了我错误你不能使用列表而不是小部件,任何人都可以解决这个问题。

【问题讨论】:

【参考方案1】:

您应该执行以下操作:

      if (snapshot.connectionState == ConnectionState.done) 
        return ListView.builder(
            shrinkWrap: true,
            itemCount: snapshot.data.documents.length,
            itemBuilder: (BuildContext context, int index) 
              return ListTile(
                contentPadding: EdgeInsets.all(8.0),
                title: Text(snapshot.data.documents[index].data["name"]),
              );
            );

假设您在文档中有name

【讨论】:

以上是关于如何在颤动中为 listview.builder 制作列表视图?的主要内容,如果未能解决你的问题,请参考以下文章