Flutter _TypeError(类型“_Map<String, dynamic>”不是类型“List<CustomerList>”的子类型)

问题描述 投票:0回答:1

 try { response = await postMethod( uri: url, body: body, mobileNo: custMobileNo, ); } catch (e) { response = null; } if (response != null && response.statusCode == 200) { try { return CustomerResponse.fromJson( convert.jsonDecode(response.data)["data"][0]); } catch (e) { CustomerResponse(custList: response.data); } } else { CustomerResponse(custList: response.data); }

我的回复文件。

工厂 CustomerResponse.fromJson(Map json) => 客户响应( 消息:json[“消息”], 状态:json[“状态”], 代码:json[“代码”], custList: json["data"] == null ? [] : 列表.from( json["data"].map((x) => CustomerList.fromJson(x))).toList(), );

Map toJson() => { “消息”:消息, “状态”:状态, “代码”:代码, “数据”:custList == null ? [] : 列表.from( custList.map((x) => x.toJson()).toList(), ), };

[![Response](https://i.stack.imgur.com/dML3n.png)](https://i.stack.imgur.com/dML3n.png)

我想发送数据到服务器。 并显示成功消息。给用户

flutter dart flutter-dependencies dio
1个回答
0
投票

您应该创建一个

CustomerList
对象并将它们添加到
custList
。有样品:

    ...
    if (response != null && response.statusCode == 200) {
      try {
        return CustomerResponse.fromJson(
            convert.jsonDecode(response.data)["data"][0]);
      } catch (e) {
        List<CustomerList> customerList = [];
        //data or customerData what name you give
        for (var item in convert.jsonDecode(response.data)["data"]) {
          customerList.add(CustomerList.fromJson(item));
        }
        CustomerResponse(custList: customerList);
        ...
      }
    }
    ...
© www.soinside.com 2019 - 2024. All rights reserved.