简体   繁体   中英

How flutter call Widget repeatedly?

I have Data List. And I want to use my Widget repeatedly as much List.length. So, I used to using for loop. 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.

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() {
    data1 = widget.GetData.data1;
    data2 = widget.GetData.data2;
    data3 = widget.GetData.data3;
  }

You need to use a ListView.builder for this:

 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.
                },
              ),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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