[英]How flutter call Widget repeatedly?
I have Data List.我有数据列表。 And I want to use my Widget repeatedly as much List.length.
而且我想重复使用我的 Widget 和 List.length 一样多。 So, I used to using for loop.
所以,我习惯于使用 for 循环。 But it doesn't work as expect.
但它没有按预期工作。 Widgets are displaying the last data.
小部件正在显示最后的数据。 Here's my code.
这是我的代码。 please check this.
请检查这个。 Thank you.
谢谢你。
The variables are temporary thing.变量是临时的东西。 And the Data is List of Json.
数据是 Json 列表。
Scaffold脚手架
return Scaffold(
body: ListView(children: [
Column(
children: [
for (int i = 0; i < Data.length; i++) ...[
Charts(GetData: Data[i]),
],
],
),
]),
);
and this is initState() in Charts这是图表中的 initState()
initState() {
data1 = widget.GetData.data1;
data2 = widget.GetData.data2;
data3 = widget.GetData.data3;
}
You need to use a ListView.builder
for this:为此,您需要使用
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.