元素类型"List< widget>"无法分配给列表类型“小部件" [英] the element type 'List<widget>' can't be assigned to the list type 'Widget'
本文介绍了元素类型"List< widget>"无法分配给列表类型“小部件"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在gridview中使用for循环添加数据,但是它显示了一些错误.这是我的组件代码
I am trying to add data using for loop in gridview but it is showing some error. Here is my code for component
return new GridView.count(
crossAxisCount: 2,
padding: const EdgeInsets.all(10.0),
crossAxisSpacing: 10.0,
mainAxisSpacing: 10.0,
children: <Widget>[getList()],
);
getList()代码
getList() code
List<Widget> getList() {
List<Widget> childs = [];
for (var i = 0; i < 10; i++) {
childs.add(new ListItem('abcd ' + $i));
}
return childs;
}
但是它显示了编译时错误.
But it is showing compile time error.
The element type 'List<widget>' can't be assigned to the list type 'Widget'
推荐答案
在这里,您将列表包装为列表
Here you are wrapping a list as list
children: <Widget>[getList()],
这应该是
children: getList(),
这篇关于元素类型"List< widget>"无法分配给列表类型“小部件"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文