元素类型 ‘List<widget>’ 不能赋值给列表类型 ‘Widget’

huangapple go评论111阅读模式
英文:

element type 'List<widget>' can't be assigned to the list type 'Widget'

问题

element type 'List<Widget>' can't be assigned to the list type 'Widget'** and second **the argument type "gridview" can't be assigned to the parameter type "iterable<Widget>".

        List<Widget> categoriesWidgets;

        String categoryCaption;
        final List<Widget> result = <Widget>[];

英文:

I am trying to output title (string) and list of items (widgets), but it is showing two errors.
one is

>element type 'List<widget>' can't be assigned to the list type 'Widget'** and second **the argument type "gridview" can't be assigned to the parameter type "iterable<widget>".

    List&lt;Widget&gt; categoriesWidgets;

    String categoryCaption,
    final List&lt;Widget&gt; result = &lt;Widget&gt;[];
    final Padding categoryTitleWidget = Padding(
    padding: const EdgeInsets.fromLTRB(0, 10, 0, 10),
    child: Text(categoryCaption));

    final GridView results = GridView.count(crossAxisCount: 2,
        children: &lt;Widget&gt;[result]
    );

    categoriesWidgets.add(categoryTitleWidget);
    categoriesWidgets.addAll(results);

    }

元素类型 ‘List<widget>’ 不能赋值给列表类型 ‘Widget’

答案1

得分: 3

你正试图创建 array of list of Widget,但期望的是 list of widget

 children: &lt;Widget&gt;[result]

修改为

 children: result

编辑: "grid view 无法分配给可迭代类型的参数"

将以下代码更改为:

categoriesWidgets.addAll([results]); // 将 results 添加到 []
英文:

You are trying to create array of list of Widget. But the expected is list of widget.

Change

 children: &lt;Widget&gt;[result]

to

 children: result

Edit: "grid view can't be assigned to parameter type iterable"

Change the following code to:

categoriesWidgets.addAll([results]); // add results within []

答案2

得分: 1

results 是一个单个小部件 GridView,它应该使用 add 而不是 addAll

  final GridView results = GridView.count(crossAxisCount: 2, children: result);
  categoriesWidgets.add(categoryTitleWidget);
  categoriesWidgets.add(results);
英文:

The results is a single widget GridView, it should use add instead of addAll.

  final GridView results = GridView.count(crossAxisCount: 2,children: result);
  categoriesWidgets.add(categoryTitleWidget);
  categoriesWidgets.add(results);

huangapple
  • 本文由 发表于 2023年3月12日 13:59:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75711312.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定