需要一个“List<dynamic>”类型的值,但得到一个“_JsonMap”类型的值
Posted
技术标签:
【中文标题】需要一个“List<dynamic>”类型的值,但得到一个“_JsonMap”类型的值【英文标题】:Expected a value of type 'List<dynamic>', but got one of type '_JsonMap' 【发布时间】:2021-07-13 17:45:24 【问题描述】:当我尝试 json 解码时,我收到错误 Expected a value of type 'List', but got one of type '_JsonMap'
我的代码:
static Future<Response<Localizacao>> getLocalizacao(String cep) async
await Future.delayed(Duration(milliseconds: 200));
try
Map<String, String> headers =
'Authorization': 'Token token=9e034db1f315356f30';
String protocol = 'https://cors-anywhere.herokuapp.com/';
String uri =
'https://www.cepaberto.com/api/v3/cep?cep=' + cep;
final endpoint = "&format=json";
String url = protocol + uri + endpoint;
final response = await http.get(url, headers: headers);
if (response.statusCode == 200)
final json = response.body;
List list = (jsonDecode(json) as List<dynamic>) ;
final local = list.map<Localizacao>((map) => Localizacao.fromJson(map)).toList();
return Response(true, msg: "OK", result: local[0]);
else
return Response(false, msg: "Erro ao conectar no web service");
catch (e)
return Response(false, msg: "Erro ao conectar no web service");
我尝试了其他方法,例如:
List list = convert.json.decode(response.body);
List list = convert.json.decode(json);
【问题讨论】:
【参考方案1】:找出问题所在。
我只需要添加:
final json = "[" + response.body + "]";
【讨论】:
【参考方案2】:试试这个
if (response.statusCode == 200)
print("IF responde==200");
final json = response.body;
print("IF rjson");
Map<String, dynamic> map= jsonDecode(json) ;
print("IF converting json");
final local = Localizacao.fromJson(map);
print("IF mapping list");
return Response(true, msg: "OK", result: local);
else
return Response(false, msg: "Erro ao conectar no web service");
catch (e)
print("error in getLocalizacao: $e");
return Response(false, msg: "Erro ao conectar no web service");
【讨论】:
"altitude":934.0,"cep":"82320520","latitude":"-25.3578899846","longitude":"-49.1953695819","logradouro":"Rua Ângela Budel" ,"bairro":"Butiatuvinha","cidade":"ddd":41,"ibge":"4106902","nome":"Curitiba","estado":"sigla":"PR"这是响应的返回,正文。我需要得到'纬度'和'经度',当我按照你说的做时,我得到:期望一个'List以上是关于需要一个“List<dynamic>”类型的值,但得到一个“_JsonMap”类型的值的主要内容,如果未能解决你的问题,请参考以下文章
需要一个“List<dynamic>”类型的值,但得到一个“_JsonMap”类型的值
我得到一个 - 类型 'List<dynamic>' 不是类型 'Map<String, dynamic>' 的子类型错误
Flutter 转换 List<List<dynamic>>
为啥 toList() 在 Dart 中创建一个 List<dynamic>?
Flutter 错误:期望一个类型为“Map<String, dynamic>”的值,但出现了一个类型为“List<dynamic>”的值