Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Listview.builder在Flatter上创建重复项_Listview_Flutter_Dynamic_Duplicates_Widget - Fatal编程技术网

Listview.builder在Flatter上创建重复项

Listview.builder在Flatter上创建重复项,listview,flutter,dynamic,duplicates,widget,Listview,Flutter,Dynamic,Duplicates,Widget,我正在创建一个动态的窗口小部件列表,但每次我添加另一个窗口小部件时,它都会重复,当我添加2个窗口小部件时,它会变成4个窗口小部件,当我添加3个窗口小部件时,它会变成9个窗口小部件,依此类推。。。我一直在网上搜索可能的原因,我认为这与此非常相似我认为我在代码中遗漏了一些东西,我不知道把r放在哪里,请帮助 child: Container( height: 250, child: new ListView.builder( physics: Clampin

我正在创建一个动态的窗口小部件列表,但每次我添加另一个窗口小部件时,它都会重复,当我添加2个窗口小部件时,它会变成4个窗口小部件,当我添加3个窗口小部件时,它会变成9个窗口小部件,依此类推。。。我一直在网上搜索可能的原因,我认为这与此非常相似我认为我在代码中遗漏了一些东西,我不知道把r放在哪里,请帮助

child: Container(
      height: 250,
      child: new ListView.builder(
          physics: ClampingScrollPhysics(),
          shrinkWrap: true,
          scrollDirection: Axis.horizontal,
          itemCount: newList.length,
          itemBuilder: (context, index) {

            final r = newList[index];

            return new Row(
              children: newList,
            );
          }),
    ),
  );
这是我的新名单:

List<Widget> newList = [];
List newList=[];

您已将列表的itemCount设置为newList length。itemBuilder代码的运行次数为长度的x倍。因此,在itemBuilder中,您需要将索引传递给正在显示的newList,否则它将显示所有列表

child: Container(
          height: 250,
          child: new ListView.builder(
              physics: ClampingScrollPhysics(),
              shrinkWrap: true,
              scrollDirection: Axis.horizontal,
              itemCount: newList.length,
              itemBuilder: (context, index) {

                final r = newList[index];

                return new Row(
                  children: newList[index],
                );
              }),
        ),
      );

感谢您的关注,我尝试使用了您的代码,但它显示参数类型“Widget”无法分配给参数类型“List”,这是因为一行包含Widget列表,而您的newList索引中只有单个Widget,如SizedBox()、Container()。请尝试删除行并返回newList[index];如果您想继续使用Row,请尝试以下操作:返回Row(children:[newList[index],]);