android – 元素类型’List’不能分配给列表类型’Widget’

前端之家收集整理的这篇文章主要介绍了android – 元素类型’List’不能分配给列表类型’Widget’前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图在gridview中使用for循环添加数据,但它显示出一些错误.这是我的组件代码

return new GridView.count(
    crossAxisCount: 2,padding: const EdgeInsets.all(10.0),crossAxisSpacing: 10.0,mainAxisSpacing: 10.0,children: <Widget>[getList()],);

getList()代码

List<Widget> getList() {
  List<Widget> childs = [];
  for (var i = 0; i < 10; i++) {
    childs.add(new ListItem('abcd ' + $i));
  }
  return childs;
}

但它显示编译时错误.

The element type 'List<widget>' can't be assigned to the list type 'Widget'

解决方法

在这里,您将列表包装为列表

children: <Widget>[getList()],

这应该是

children: getList(),

猜你在找的Flutter相关文章