首页 > 解决方案 > 如何在 DART 中将 MAP 转换为 LIST?

问题描述

我无法将 MAP 转换为 DART 上的 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, 
    ........
    }
  ]
}

标签: listflutterdictionarydart

解决方案


您的 JSON 数据不是 aList<Map<String, dynamic>>而是Map<String, List<Map<String, dynamic>>>: { 'meus_testes_vw': [...] }

尝试更改您的分配listaResponse

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

推荐阅读