私はこのようなプロバイダークラスを持っています、
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>
タイプでもあるので、理由は何でしょうか?
タイプのない空の配列(「動的」)を設定するのではなく、正しいタイプの空の配列を設定する必要があります。
theDataProvider.habitsList = <Widget>[]
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加