【发布时间】:2021-01-10 19:01:46
【问题描述】:
我有一个这样的提供者类,
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 在提供程序类中也是 List<Widget> 类型?
【问题讨论】:
标签: flutter types flutter-provider