简体   繁体   English

flutter如何反复调用Widget?

[英]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.

相关问题 当 flutter 中的小部件重建时,如何在不重复调用 api 的情况下在本地小部件中获取天气信息? - How to get weather information in a local widget without calling api repeatedly as widget rebuilds in flutter? 当基于新输入更新小部件的内容时,如何将 animation 重复应用于 Flutter 中的无状态小部件? - How to repeatedly apply animation to a Stateless Widget in Flutter when the widget's content is updated based on new input? 如何在小部件中调用动态方法 - flutter - How to call dynamic method in widget - flutter Flutter:如何从其他小部件调用 openDrawer - Flutter: How to call openDrawer from other widget Flutter 如何以编程方式调用自定义小部件中的方法? - Flutter how to programmatically call method in custom widget? Flutter:如何从不是小部件的类中调用 SnackBar - Flutter: How to call a SnackBar form a class that is not a widget 如何从 Hero 小部件调用提供程序 - Flutter - How to call a provider from a Hero widget - Flutter 如何从小部件调用颤动小部件的状态 - How to call into a flutter widget's state from the widget 如何在另一个 stful 小部件颤动中调用 stful 小部件中的方法 - how to call method in a stful widget inside a another stful widget flutter 如何从 Flutter 中的子小部件调用父小部件 function - How to call parent widget function from child widget in Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM