【问题标题】:The argument type 'List<Iterable<DropdownMenuItem<int>>>' can't be assigned to the parameter type 'List<DropdownMenuItem<dynamic>>'参数类型“List<Iterable<DropdownMenuItem<int>>>”不能分配给参数类型“List<DropdownMenuItem<dynamic>>”
【发布时间】:2019-10-05 06:51:05
【问题描述】:

在颤振中,我正在使用 TMDB API 并从那里获取流派名称,并希望将它们显示在我的 DropdownButton 上,但出现此错误。

参数类型List>>不能赋值 到参数类型'List>

{
  "genres": [
    {
      "id": 28,
      "name": "Action"
    },
    {
      "id": 12,
      "name": "Adventure"
    },
    {
      "id": 16,
      "name": "Animation"
    }, .......]
}

这是 json。

return DropdownButton(
              items: [ snapGenre.data.genres.map((map) => DropdownMenuItem(child: Text(map.name),value: map.id,))]
                      );
                    }

这部分是我尝试过的。

简而言之,我想根据从 API 获得的流派名称返回/创建一个 dropDownMenuItem。我错过了哪一部分,请您帮帮我。

【问题讨论】:

    标签: flutter


    【解决方案1】:

    您通过使用[] 声明List,然后在其中映射您的数据,因此您最终在List 中得到了Iterable。相反,您必须删除 [] 并将 Iterable 最后转换为 List:

    return DropdownButton(
      items: snapGenre.data.genres.map((map) => DropdownMenuItem(
          child: Text(map.name),
          value: map.id,
        ),
      ).toList(),
    );
    

    【讨论】:

    • 我没有意识到那个棘手的部分。感谢您的帮助和解释。
    猜你喜欢
    • 1970-01-01
    • 2021-11-13
    • 2021-12-24
    • 1970-01-01
    • 2021-10-24
    • 2021-09-15
    • 2021-10-18
    • 2020-05-28
    • 2019-11-07
    相关资源
    最近更新 更多