【问题标题】:type 'List<dynamic>' is not a subtype of type 'List<Widget>' flutter carousel类型“List<dynamic>”不是“List<Widget>”类型的子类型颤动轮播
【发布时间】:2021-01-10 23:55:27
【问题描述】:

我正在使用轮播,我正在像这样在轮播的 items 参数中分配List&lt;Widgets&gt;

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(),

这是列表,

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

我在这里将小部件添加到这样的列表中,

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

虽然我通过了List&lt;Widget&gt;为什么会出现这样的错误,

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

我错过了什么?

【问题讨论】:

  • 尝试更改项目:theDataProvider.habitsList.map((i) { 为项目:theDataProvider.habitsList.map((i) {
  • 非常感谢@KrisLeland

标签: flutter types flutter-layout


【解决方案1】:

请参阅this other Stack answer 来帮助解释它,但 Dart 映射函数返回一个动态类型,除非显式强制转换。

尝试改变

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

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

【讨论】:

    猜你喜欢
    • 2021-01-10
    • 2018-09-11
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 2021-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多