Aman Chaudhary
Aman Chaudhary

Reputation: 912

type 'List<dynamic>' is not a subtype of type 'List<Widget>' of 'val' flutter

I have a provider class like this,

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();
  }
}

here in the function addingHabits I am adding a widget in the List<Widget> habitsList. And the function is returning the 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,
      ),
    );
  }

And I am just calling the function inside the column like this, widget.addingHabits().
But it gives an error, like this

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>

What might be the reason, as the habitsList is also of List<Widget> type in provider class?

Upvotes: 1

Views: 296

Answers (1)

nvoigt
nvoigt

Reputation: 77354

You should not set an empty array with no type (which would be "dynamic"), but an empty array of the correct type:

theDataProvider.habitsList = <Widget>[]

Upvotes: 2

Related Questions