【问题标题】:Flutter Listview not displaying all itemsFlutter Listview不显示所有项目
【发布时间】:2021-08-18 20:25:46
【问题描述】:

我有一个列表视图,它应该显示来自 firebase 的项目流。在每两个项目之后,都会显示一个自定义小部件,并使用 if else 语句来实现这一点。现在的问题是我的自定义小部件正在覆盖来自 firebase 的一些项目,因此如果流中总共有 5 个项目,我只会得到 3 个,而另外 2 个正在被我的自定义小部件替换。有没有办法获取包括自定义小部件在内的所有项目,以便(按照上面的示例)我总共可以拥有 7 个项目?这是我的参考代码

StreamBuilder(
            stream: stream,
            //stream: getposts(),
            builder: (BuildContext context,
                AsyncSnapshot<List<DocumentSnapshot>>snapshots) {
return Flexible(
                  child: ListView.builder(
                    scrollDirection: Axis.vertical,
                    shrinkWrap: true,
                    itemBuilder: (context, index) {

                    if(index % 2 == 0 && index != 0){
                       return Container(child: Text("Custom Widget")) //this is the code inserting custom widget into the listview
                    else{ Container(child: Text(snapshots.data.title))
}

}))
}

【问题讨论】:

    标签: flutter listview


    【解决方案1】:

    你能用这样的列吗(?)

    if(index % 2 == 0 && index != 0){
      return Column(
        children: [
          Container(child: Text("Custom Widget")),
          Container(child: Text(snapshots.data.title)),
        ]
      );
    } else {
      return Container(child: Text(snapshots.data.title))
    }
    

    【讨论】:

      猜你喜欢
      • 2021-05-13
      • 1970-01-01
      • 2017-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-30
      相关资源
      最近更新 更多