【问题标题】:type 'List<Widget>' is not a subtype of type 'Widget' in a dynamic attribution of widgets类型“List<Widget>”不是小部件动态属性中“Widget”类型的子类型
【发布时间】:2021-04-15 12:23:03
【问题描述】:

我有这个代码:

child: Column(
  children: [
    Text(i.description),
    i.colors.map<Widget>(
    (colours) => CheckboxListTile(
      title: Text(colors),
      value: i[colors],
      onChanged: (colors){
        setState(() {
          i[colors] = colors;
        });
      },
    ).toList()
   ],
  )

我想为每个对象中存在的每种颜色显示检查按钮,但它会引发此错误:

type 'List ' 不是 type 'Widget' 的子类型

我该如何解决?

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    您收到错误是因为 Column.children 需要一个“简单”数组。但是你给了一个带有Text 和另一个数组 (i.colors.map&lt;&gt;(___).toList()) 的数组,所以你得到了错误。

    在您的列表之前添加 扩展运算符 ...,它将打开它:

    child: Column(
      children: [
        Text(i.description),
        ...i.colors.map<Widget>(
        (colours) => CheckboxListTile(
          title: Text(colors),
          value: i[colors],
          onChanged: (colors){
            setState(() {
              i[colors] = colors;
            });
          },
        ).toList()
       ],
      )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-04
      • 2020-04-07
      • 1970-01-01
      • 2018-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多