本文介绍了期望类型为“List<dynamic>"的值,但得到类型为“_JsonMap"的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试对 json 解码时,我收到错误期望类型为列表"的值,但得到类型为_JsonMap"的值

When I try to json decode I get the error 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");
    }
  }

我尝试了其他方法,例如:

I tried other ways like:

List list = convert.json.decode(response.body);
List list = convert.json.decode(json);

推荐答案

找出问题所在.

我只需要补充:

final json = "[" + response.body + "]";

这篇关于期望类型为“List<dynamic>"的值,但得到类型为“_JsonMap"的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 16:28