list - 如何在 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,
........
}
]
}
解决方案
您的 JSON 数据不是 aList<Map<String, dynamic>>
而是Map<String, List<Map<String, dynamic>>>
: { 'meus_testes_vw': [...] }
。
尝试更改您的分配listaResponse
:
List listaResponse = jsonDecode(response.body)['meus_testes_vw'];
推荐阅读
- r - rmarkdown::render 不起作用,但在编织时起作用
- zend-framework - 无法将服务“Zend\Db\Adapter\AdapterInterface”解析为工厂
- swift4 - 表视图没有填充数据,但我的打印语句正在工作
- asp.net - 在 IIS 中发布 Web 应用程序得到不同的错误
- python - 熊猫中两个日期列之间的比较
- angular - (window as any).global = window;
- wordpress - Wordpress 图像名称区分大小写给出 404 错误
- ios - ios + 带有麦克风的录音蓝牙设备(airpod)
- android - 使用 volley 获取 json 对象和数组
- javascript - 使用 toJSON() 访问对象的属性