【发布时间】:2021-01-10 23:55:27
【问题描述】:
我正在使用轮播,我正在像这样在轮播的 items 参数中分配List<Widgets>,
items: theDataProvider.habitsList.map((i) {
return Builder(
builder: (BuildContext context) {
return Container(
width: MediaQuery.of(context).size.width,
margin: EdgeInsets.symmetric(horizontal: 5.0),
decoration: BoxDecoration(color: Colors.amber),
child: i);
},
);
}).toList(),
这是列表,
List<Widget> _habitsList = [];
List<Widget> get habitsList => _habitsList;
set habitsList(List<Widget> val) {
_habitsList = val;
notifyListeners();
}
我在这里将小部件添加到这样的列表中,
addingHabits() {
theDataProvider.habitsList = <Widget>[];
for (int i = 0; i < theDataProvider.myHabits.length; i++) {
theDataProvider.habitsList.add(Column(
children: [
Container(
height: 65,
width: 65,
decoration: BoxDecoration(
color: theDataProvider.myHabits[i].boxColor,
border: Border.all(
color: Colors.grey,
),
borderRadius: BorderRadius.all(Radius.circular(16))),
child: Image(image: theDataProvider.myHabits[i].boxImage),
),
Text(
theDataProvider.myHabits[i].title,
style: TextStyle(color: Colors.white),
)
],
));
}
return Expanded(
child: ListView(
scrollDirection: Axis.horizontal,
children: theDataProvider.habitsList,
),
);
}
虽然我通过了List<Widget>为什么会出现这样的错误,
type 'List<dynamic>' is not a subtype of type 'List<Widget>'
我错过了什么?
【问题讨论】:
-
尝试更改项目:theDataProvider.habitsList.map((i) { 为项目:theDataProvider.habitsList.map
((i) { -
非常感谢@KrisLeland
标签: flutter types flutter-layout