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

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

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

问题

  1. 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>".
  2. List<Widget> categoriesWidgets;
  3. String categoryCaption;
  4. 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>".

  1. List&lt;Widget&gt; categoriesWidgets;
  2. String categoryCaption,
  3. final List&lt;Widget&gt; result = &lt;Widget&gt;[];
  1. final Padding categoryTitleWidget = Padding(
  2. padding: const EdgeInsets.fromLTRB(0, 10, 0, 10),
  3. child: Text(categoryCaption));
  4. final GridView results = GridView.count(crossAxisCount: 2,
  5. children: &lt;Widget&gt;[result]
  6. );
  7. categoriesWidgets.add(categoryTitleWidget);
  8. categoriesWidgets.addAll(results);
  9. }

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

答案1

得分: 3

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

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

修改为

  1. children: result

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

将以下代码更改为:

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

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

Change

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

to

  1. children: result

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

Change the following code to:

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

答案2

得分: 1

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

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

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

  1. final GridView results = GridView.count(crossAxisCount: 2,children: result);
  2. categoriesWidgets.add(categoryTitleWidget);
  3. 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:

确定