【问题标题】:type 'List<dynamic>' is not a subtype of type 'List<Widget>' of 'val' flutter类型“List<dynamic>”不是“val”颤振的“List<Widget>”类型的子类型
【发布时间】:2021-01-10 19:01:46
【问题描述】:

我有一个这样的提供者类,

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&lt;Widget&gt; 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&lt;Widget&gt; 类型?

【问题讨论】:

    标签: flutter types flutter-provider


    【解决方案1】:

    你不应该设置一个没有类型的空数组(这将是“动态的”),而是一个正确类型的空数组:

    theDataProvider.habitsList = <Widget>[]
    

    【讨论】:

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