繁体   English   中英

输入&#39;列表<dynamic> &#39; 不是 &#39;List 类型的子类型<Widget> &#39; &#39;val&#39; 颤动

[英]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.

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