【问题标题】:How to convert a MAP into a LIST in DART?如何在 DART 中将 MAP 转换为 LIST?
【发布时间】:2021-06-07 07:50:11
【问题描述】:

在 DART 上将 MAP 转换为 LIST 时遇到问题。

我的代码如下所示:

try {
      var response = await http.get(url, headers: header);

      List listaResponse = jsonDecode(response.body);

      final lockers = <Locker>[];

      for (Map map in listaResponse) {
        Locker l = Locker.fromJson(map);
        lockers.add(l);
      }
      return lockers;
    } catch (e) {
      print("error in api ");
      print(e);
    }

这是输出错误:

I/flutter (14625): type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>'

这就是我的 json 的样子:

{
meus_testes_vw: [
    {
    id_teste = 73,
    otherinfo = ahsuasa 
    ........
    },
    {
    id_teste = 74, 
    ........
    }
  ]
}

【问题讨论】:

    标签: list flutter dictionary dart


    【解决方案1】:

    您的 JSON 数据不是List&lt;Map&lt;String, dynamic&gt;&gt;,而是Map&lt;String, List&lt;Map&lt;String, dynamic&gt;&gt;&gt;{ 'meus_testes_vw': [...] }

    尝试更改listaResponse的分配:

    List listaResponse = jsonDecode(response.body)['meus_testes_vw'];
    

    【讨论】:

    • 非常感谢。真的非常感谢!!!!解决了。我有这个问题两天了
    猜你喜欢
    • 2017-11-02
    • 2020-12-18
    • 2020-04-08
    • 2017-07-14
    • 2021-03-27
    • 2020-10-04
    • 2013-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多