簡體   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