带有列表中的小部件的 Flutter ListView 在它应该更新时不会更新 [英] Flutter ListView with widgets in a List doesn't updates when it should
问题描述
我正在尝试将要由 ListView 构建的小部件存储在这样的全局列表中:
I am trying to store Widgets to build by a ListView in a global List like this:
List<Widget> globalList= [];
我正在尝试在像这样的有状态类的状态下构建 Listview:
Im trying to build a Listview in the state of a stateful class like this:
ListView(
children: globalList
)
现在我每次都尝试在将项目添加到列表时呈现此列表视图.我为此使用的功能是:
Now im trying to render this Listview everytime, when an item is added to the list. The function i use for this is:
void addList(){
setState((){
Widget _widget = new Widget();
list.add(_widget);
});
小部件(在我的伪代码中称为 _widget
)也是一个有状态的小部件.但是当我调用 addList
时,我的有状态小部件不会更新.为什么?
The Widget (in my pseudocode called _widget
) is also a Stateful widget. But when i call addList
, my stateful widget does not update. Why?
缩短(伪)代码:
List<Widget> globalList = [];
class STFUL extends StatefulWidget{
@override
_STFULState createState() => _STFULState();
}
class _STFULState extends State<STFUL>{
void addList(){
setState((){
Widget _widget = new Widget();
globalList.add(_widget);
});
@override
Widget build(BuildContext context) {
return Scaffold(
body:ListView(
children: globalList
)
}
}
下面有一个调用 addList 的按钮.我的问题是,当我调用 setState 时,ListView 不会呈现更新的列表.
Below there is a button to call addList. My problem is, that the ListView doesn't renders the updated list, when I call setState.
感谢您的帮助
推荐答案
使用 ListView.Builder
代替 ListView
,
这篇关于带有列表中的小部件的 Flutter ListView 在它应该更新时不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!