繁体   English   中英

flutter如何反复调用Widget?

[英]How flutter call Widget repeatedly?

我有数据列表。 而且我想重复使用我的 Widget 和 List.length 一样多。 所以,我习惯于使用 for 循环。 但它没有按预期工作。 小部件正在显示最后的数据。 这是我的代码。 请检查这个。 谢谢你。

变量是临时的东西。 数据是 Json 列表。

脚手架

return Scaffold(
      body: ListView(children: [
        Column(
          children: [
            for (int i = 0; i < Data.length; i++) ...[
              Charts(GetData: Data[i]),
            ],
          ],
        ),
      ]),
    );

这是图表中的 initState()

initState() {
    data1 = widget.GetData.data1;
    data2 = widget.GetData.data2;
    data3 = widget.GetData.data3;
  }

为此,您需要使用ListView.builder

 ListView.builder(
                itemCount: Data.length,   //you just need to specify the amount of items
                itemBuilder: (BuildContext context, int index) {
                  return Charts(GetData: Data[i]); //and then this line will return Data.length amount of Charts in that ListView.
                },
              ),

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM