'List<dynamic>' 类型不是 'val' 类型 'List<Widget>' 的子类型 flutter
type 'List<dynamic>' is not a subtype of type 'List<Widget>' of 'val' flutter
我有这样的供应商class,
class TheData extends ChangeNotifier {
//1) For storing widgets of habitcontainer
List<Widget> _habitsList = [];
List<Widget> get habitsList => _habitsList;
set habitsList(List<Widget> val) {
_habitsList = val;
notifyListeners();
}
}
在函数 addingHabits
中,我在 List<Widget> habitsList
中添加了一个小部件。函数返回 ListView
.
addingHabits() {
theDataProvider.habitsList = [];
for (int i = 0; i < myHabits.length; i++) {
theDataProvider.habitsList.add(Column(
children: [
Container(
height: 65,
width: 65,
decoration: BoxDecoration(
color: myHabits[i].boxColor,
border: Border.all(
color: Colors.grey,
),
borderRadius: BorderRadius.all(Radius.circular(16))),
child: Image(image: myHabits[i].boxImage),
),
Text(
myHabits[i].title,
style: TextStyle(color: Colors.white),
)
],
));
}
return Expanded(
child: ListView(
scrollDirection: Axis.horizontal,
children: theDataProvider.habitsList,
),
);
}
我只是像这样调用列内的函数,widget.addingHabits()
。
但是它给出了一个错误,像这样
The following _TypeError was thrown building Consumer<TheData>(dirty, dependencies:
[_InheritedProviderScope<TheData>]):
type 'List<dynamic>' is not a subtype of type 'List<Widget>' of 'val'
The relevant error-causing widget was:
Consumer<TheData>
可能是什么原因,因为 habitsList
在提供商 class 中也是 List<Widget>
类型?
您不应设置没有类型的空数组(这将是“动态”),而应设置正确类型的空数组:
theDataProvider.habitsList = <Widget>[]
我有这样的供应商class,
class TheData extends ChangeNotifier {
//1) For storing widgets of habitcontainer
List<Widget> _habitsList = [];
List<Widget> get habitsList => _habitsList;
set habitsList(List<Widget> val) {
_habitsList = val;
notifyListeners();
}
}
在函数 addingHabits
中,我在 List<Widget> habitsList
中添加了一个小部件。函数返回 ListView
.
addingHabits() {
theDataProvider.habitsList = [];
for (int i = 0; i < myHabits.length; i++) {
theDataProvider.habitsList.add(Column(
children: [
Container(
height: 65,
width: 65,
decoration: BoxDecoration(
color: myHabits[i].boxColor,
border: Border.all(
color: Colors.grey,
),
borderRadius: BorderRadius.all(Radius.circular(16))),
child: Image(image: myHabits[i].boxImage),
),
Text(
myHabits[i].title,
style: TextStyle(color: Colors.white),
)
],
));
}
return Expanded(
child: ListView(
scrollDirection: Axis.horizontal,
children: theDataProvider.habitsList,
),
);
}
我只是像这样调用列内的函数,widget.addingHabits()
。
但是它给出了一个错误,像这样
The following _TypeError was thrown building Consumer<TheData>(dirty, dependencies:
[_InheritedProviderScope<TheData>]):
type 'List<dynamic>' is not a subtype of type 'List<Widget>' of 'val'
The relevant error-causing widget was:
Consumer<TheData>
可能是什么原因,因为 habitsList
在提供商 class 中也是 List<Widget>
类型?
您不应设置没有类型的空数组(这将是“动态”),而应设置正确类型的空数组:
theDataProvider.habitsList = <Widget>[]