简体   繁体   中英

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

I am using a carousel and I am assigning a List<Widgets> in the items parameter of the carousel like this,

items: theDataProvider.habitsList.map((i) {
          return Builder(
            builder: (BuildContext context) {
              return Container(
                  width: MediaQuery.of(context).size.width,
                  margin: EdgeInsets.symmetric(horizontal: 5.0),
                  decoration: BoxDecoration(color: Colors.amber),
                  child: i);
            },
          );
        }).toList(),

Here is the list,

  List<Widget> _habitsList = [];
  List<Widget> get habitsList => _habitsList;
  set habitsList(List<Widget> val) {
    _habitsList = val;
    notifyListeners();
  }

And here I am adding widgets to the list like this,

addingHabits() {
    theDataProvider.habitsList = <Widget>[];
    for (int i = 0; i < theDataProvider.myHabits.length; i++) {
      theDataProvider.habitsList.add(Column(
        children: [
          Container(
            height: 65,
            width: 65,
            decoration: BoxDecoration(
                color: theDataProvider.myHabits[i].boxColor,
                border: Border.all(
                  color: Colors.grey,
                ),
                borderRadius: BorderRadius.all(Radius.circular(16))),
            child: Image(image: theDataProvider.myHabits[i].boxImage),
          ),
          Text(
            theDataProvider.myHabits[i].title,
            style: TextStyle(color: Colors.white),
          )
        ],
      ));
    }
    return Expanded(
      child: ListView(
        scrollDirection: Axis.horizontal,
        children: theDataProvider.habitsList,
      ),
    );
  }

Although I pass the List<Widget> why does it give an error like this,

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

Where am I missing?

See this other Stack answer to help explain it, but the Dart map function returns a type dynamic unless explicitly cast.

try changing

items: theDataProvider.habitsList.map((i) {

to

items: theDataProvider.habitsList.map<Widget>((i) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM