![](/img/trans.png)
[英]The return type 'Widget?' isn't a 'Widget', as required by the closure's context
[英]The return type 'List<ListTile>' isn't a 'Widget', as required by the closure's context
我正在尝试一起使用 ListView.builder 和地图。 可以一起用吗?? 我检查了没有 listView.builder 的地图代码,它工作正常。 当我将此代码与 listview.builder 一起使用来获取索引值时,我遇到了错误。 错误在哪里?
final nameAndRollNo = [
{
'name': 'Name 1',
'details': [
{'Roll No.': 'F101', 'age': 20,},
]
},
{
'name': 'Name 2',
'details': [
{'Roll No.': 'F102', 'age': 25,},
]
},
```
ListView.builder(
itemCount: nameAndRollNo.length,
itemBuilder: (context, index) {
return (nameAndRollNo[index]['details'] as List<Map<String, dynamic>>)
.map((value) {
return ListTile(
leading: Icon(Icons.person),
title: Text('Any Name'),
subtitle: Text(value['Roll No.']),
trailing: Icon(Icons.delete),
);
}).toList();
},
),
您可以从itemBuilder
返回单个小部件,但在执行地图和创建List<ListTile>
时,意味着它无法返回小部件列表。 如果您想将它们包装为 List,您可以使用 Column 小部件包装项目。
itemBuilder: (context, index) {
return Column(
children: (nameAndRollNo[index]['details']
as List<Map<String, dynamic>>)
.map((value) {
return ListTile(
leading: Icon(Icons.person),
title: Text('Any Name'),
subtitle: Text(value['Roll No.']),
trailing: Icon(Icons.delete),
);
}).toList(),
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.