![](/img/trans.png)
[英]Flutter - Type List<Dynamic> is not subtype of type Widget
[英]type 'List<dynamic>' is not a subtype of type 'List<Widget>' of 'val' flutter
我有一個像這樣的提供者類,
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
添加了一個小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>
也是提供程序類中的List<Widget>
類型?
你不應該設置一個沒有類型的空數組(這將是“動態的”),而是一個正確類型的空數組:
theDataProvider.habitsList = <Widget>[]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.