【问题标题】:How do I loop and render through a list of predefined widgets?如何循环和呈现预定义小部件列表?
【发布时间】:2021-09-07 21:15:17
【问题描述】:

所以我有一个我在下面定义的预定义选项列表

final List<DropdownMenuItem> grades = <DropdownMenuItem>[
    DropdownMenuItem(value: "Highschool", child: Text('Highschool')),
    DropdownMenuItem(value: "Senior High", child: Text('Senior High')),
    DropdownMenuItem(value: "College", child: Text('College'))];

而我要做的就是遍历列表,放到下拉菜单下

 Form(
          child: DropdownButtonHideUnderline(
              child: ButtonTheme(
            alignedDropdown: true,
            child: DropdownButton<String>(
              value: defState,
              onChanged: (String? newValue) {
                setState(() {
                  defState = newValue!;
                });
              },
              items: grades.map((items) => return items),
              hint: Text("Grade"),
            ),
          )),
        )

但它给了我这两个错误

The argument type 'Iterable<DropdownMenuItem<dynamic>>' can't be assigned to the parameter type 'List<DropdownMenuItem<String>>?'.

Unexpected text 'return'.
Try removing the text.

有没有办法迭代列表以便我可以立即返回?

【问题讨论】:

  • 或者只是items: grades
  • 都试过了还是一样的错误

标签: list flutter dart mobile widget


【解决方案1】:

一些事情:

首先,箭头函数不需要return 关键字。比如下面两个函数是等价的:

grades.map((_) => 'hello')

grades.map((_) {
  return 'hello';
})

其次,错误消息说:“你给了我一个Iterable&lt;DropdownMenuItem&lt;dynamic&gt;&gt;,但我需要一个List&lt;DropdownMenuItem&lt;String&gt;&gt;”。

您正在创建一个DropdownButton&lt;String&gt;,它将List&lt;DropdownMenuItem&lt;String&gt;&gt; 作为其items 参数,但您传入grades.map( ... )

map()Iterable 中定义,并具有以下签名:

Iterable<T> map<T>(T f(E e))

这有点不透明,但至关重要的是,它返回一个Iterable&lt;T&gt;,其中T 是您传入的函数的返回类型,例如:

List<int> ints = [1, 2, 3];
Iterable<String> strings = ints.map((i) => i.toString());

要解决您的问题,您应该执行以下操作:

  1. 与其内部包含小部件的最终字段,不如使其成为一个函数。您应该避免缓存这样的小部件(除非它们是const),因为它会干扰热重载。重要的是,该函数还应该指定它返回的DropdownMenuItem 的类型:
List<DropdownMenuItem<String>> get grades => [
  DropdownMenuItem<String>(...),
  ...
]
  1. 您的 map 函数无效,但如果您删除 return 关键字,它将是 (items) =&gt; items,这是标识函数(即它什么都不做)。您可以删除它,它会起作用。 或者,如果您想调用某个函数,则需要在结果上调用toList(),因为items 需要一个列表:
  items: grades,
   
  // or

  items: grades.map((grade) => ...).toList(),

【讨论】:

    【解决方案2】:

    grades列表的类型改为List&lt;DropdownMenuItem&lt;String&gt;&gt;

      final List<DropdownMenuItem<String>> grades = [
        DropdownMenuItem(value: "Highschool", child: Text('Highschool')),
        DropdownMenuItem(value: "Senior High", child: Text('Senior High')),
        DropdownMenuItem(value: "College", child: Text('College'))
      ];
    

    只需使用grades 而不是地图:

    DropdownButton<String>(
      value: defState,
      onChanged: (String? newValue) {
        setState(() {
          defState = newValue!;
        });
      },
      items: grades,
      hint: Text("Grade"),
    ),
    

    【讨论】:

      猜你喜欢
      • 2011-12-16
      • 2018-12-09
      • 2021-12-02
      • 2020-07-20
      • 2011-08-30
      • 2014-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多