【问题标题】:type 'List<void>' is not a subtype of type 'List<DropdownMenuItem<Client>>'“List<void>”类型不是“List<DropdownMenuItem<Client>>”类型的子类型
【发布时间】:2020-01-25 02:24:31
【问题描述】:

我只是通过循环获取以下内容的 API 请求的结果来打印 DropdownButton 中的内容时遇到问题:

[{id: 1, nome: foo}, ...]

这是它的代码。

return _response.data.map<Client>((i) => Client.fromJson(i)).toList(); 

奇怪的是,当打印存储上述调用的变量是[Instance of 'Client', ...]

然后,在视图中,我尝试至少打印每个项目:

DropdownButton<Client>(
  onChanged: (client) => print(client),
  items: _controller.clients
      .map(
        (i) => print(i),
      )
      .toList(),

但是type 'List&lt;void&gt;' is not a subtype of type 'List&lt;DropdownMenuItem&lt;Client&gt;&gt;'。我已经迷路了。

【问题讨论】:

    标签: list flutter dart


    【解决方案1】:

    在打印 i 的地方,您需要返回 DropDownMenuItem(带孩子)

    例如:

      items: _controller.clients.map((e) => DropDownMenuItem(value: e, child: Text(e.nome))).toList(),
    

    【讨论】:

    • 谢谢,我没有意识到这是必须的。以前,即使使用DropdownMenuItem,我也会遇到类似的错误消息。
    猜你喜欢
    • 1970-01-01
    • 2019-11-07
    • 2021-11-05
    • 1970-01-01
    • 2020-12-12
    • 2021-06-24
    • 2021-07-06
    • 2021-02-17
    • 2018-09-11
    相关资源
    最近更新 更多