Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Performance 如何在Flatter中有效地将小部件列表传递到listview?_Performance_Flutter_Listview_Dart - Fatal编程技术网

Performance 如何在Flatter中有效地将小部件列表传递到listview?

Performance 如何在Flatter中有效地将小部件列表传递到listview?,performance,flutter,listview,dart,Performance,Flutter,Listview,Dart,方法get候选者()返回我正在传递到列表视图中的小部件列表,如下所示。当我检查我的dartDevTools内存快照时,只有3个候选应用程序,并且该应用程序的性能非常差。关于如何提高性能,有什么建议吗 List<Widget> getCandidates() { List<Widget> recentCandidates = []; List<Candidate> candidates = candidateList(); for (Candidat

方法get候选者()返回我正在传递到列表视图中的小部件列表,如下所示。当我检查我的dartDevTools内存快照时,只有3个候选应用程序,并且该应用程序的性能非常差。关于如何提高性能,有什么建议吗

List<Widget> getCandidates() {
  List<Widget> recentCandidates = [];
  List<Candidate> candidates = candidateList();
  for (Candidate candidate in candidates) {
    recentCandidates.add(CandidateCard(candidate: candidate));
  }
  return recentCandidates;
}
传递小部件列表并不是一种真正的“最有效的方法”,而是需要多少行代码的方法。以下是我只想用几行文字来表达的方式:

列出候选人=候选名单();
容器(
身高:200,
子:ListView(
AddAutomaticKepaLifes:false,
填充:仅限边缘设置(左:30),
滚动方向:轴水平,
儿童:[
…candidates.map((Candidate e)=>CandidateCard(Candidate:e)).toList(),
],
),
)
通过这样做,您可以保持代码的小型化,并且(在我看来)仍然易于理解。您不需要像使用
recentCandidates
那样使用临时变量,也不需要创建专用的方法。

传递小部件列表的方法实际上不是“最有效的方法”,而是占用更多或更少代码行的方法。以下是我只想用几行文字来表达的方式:

列出候选人=候选名单();
容器(
身高:200,
子:ListView(
AddAutomaticKepaLifes:false,
填充:仅限边缘设置(左:30),
滚动方向:轴水平,
儿童:[
…candidates.map((Candidate e)=>CandidateCard(Candidate:e)).toList(),
],
),
)

通过这样做,您可以保持代码的小型化,并且(在我看来)仍然易于理解。您不需要像使用
recentCandidates
那样使用临时变量,也不需要创建专用的方法。

在单独的方法上构建listview是没有效率的。您所能做的是创建一个方法来构建一个小部件并递归调用它

你也可以参考我觉得有用的YouTube视频

供参考

将它放在构建方法中,您可以删除行,并用您想要的任何小部件(如column或listview)替换它

 Container(
                  child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: List<Widget>.generate(items.length,
                          (index) => makeItem(items[index], index))),
                ),
              ),

在单独的方法上构建listview是无效的。您所能做的是创建一个方法来构建一个小部件并递归调用它

你也可以参考我觉得有用的YouTube视频

供参考

将它放在构建方法中,您可以删除行,并用您想要的任何小部件(如column或listview)替换它

 Container(
                  child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: List<Widget>.generate(items.length,
                          (index) => makeItem(items[index], index))),
                ),
              ),
 Widget makeItem(Map widget, int index) {
    if (index == _selectedIndex) {
      return YourIndexBasedWidget()
    } else {
      return OtherWidget();
    }
  }