解析JSON(地图列表)将返回null

时间:2020-07-31 07:49:44

标签: json flutter dart

我制作了模型类来解析JSON。我正在尝试解析地图列表。但是由于某种原因,它返回null。 当我打印API响应时,就可以了 当我打印解码的API响应时,也可以 当我从解码的地图打印值时,也可以 但是当我使用这些类时,它不起作用。

有人可以帮忙,为什么会这样?

模型

class CustomerAddressList {
  List<CustomerAddress> customerAddressList;
  CustomerAddressList({this.customerAddressList});

  factory CustomerAddressList.fromJson(List<dynamic> json) {
    List<CustomerAddress> customerAddressL = List<CustomerAddress>();
    customerAddressL = json.map((i) => CustomerAddress.fromJson(i)).toList();
    return CustomerAddressList(customerAddressList: customerAddressL);
  }
}

class CustomerAddress {
  int id;
  int customerId;
  String addressType;
  String address1;
  String address2;
  String postcode;
  int distance;
  String mobile;
  String activationStatus;
  String createdAt;
  CustomerAddress({
    id,
    customerId,
    addressType,
    address1,
    address2,
    postcode,
    distance,
    mobile,
    activationStatus,
    createdAt,
  });
  factory CustomerAddress.fromJson(Map<String, dynamic> json) {
    return CustomerAddress(
      id: json['id'],
      customerId: json['customer_id'],
      addressType: json['address_type'],
      address1: json['address1'],
      address2: json['address2'],
      postcode: json['postcode'],
      distance: json['distance'],
      mobile: json['mobile'],
      activationStatus: json['activation_status'],
      createdAt: json['created_at'],
    );
  }
}

API呼叫

try {
      var response =
          await http.get(baseUrl + "addressbyidcustomer/$id", headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json',
        'Authorization': 'Bearer $token',
      });
      // print(response.body);
      if (response.statusCode == 200) {
        List<dynamic> data = json.decode(response.body);
        customerAddressObj = CustomerAddress.fromJson(data[0]);
        print(data[0]['address1']);
        print(data[0]['address2']);
        print(data[0]['address_type']);
        customerAddressListObj = CustomerAddressList.fromJson(data);
        print("customerAddressObj: " + customerAddressObj.address1.toString());
        print("customerAddressObj: " + customerAddressObj.address2.toString());
        print(
            "customerAddressObj: " + customerAddressObj.addressType.toString());
      }

它们可以完美工作并打印数据

print(data[0]['address1']);
print(data[0]['address2']);
print(data[0]['address_type']);

但这些打印为空

print("customerAddressObj: " + customerAddressObj.address1.toString());
print("customerAddressObj: " + customerAddressObj.address2.toString());
print("customerAddressObj: " + customerAddressObj.addressType.toString());

控制台日志

I/flutter ( 5357): London, UK
I/flutter ( 5357): London, UK
I/flutter ( 5357): Work
I/flutter ( 5357): customerAddressObj: null
I/chatty  ( 5357): uid=10095(com.example.ControlPAD) Thread-2 identical 1 line
I/flutter ( 5357): customerAddressObj: null

API响应

[
    {
        "id": 15,
        "customer_id": 501,
        "address_type": "Work",
        "address1": "London, UK",
        "address2": "London, UK",
        "postcode": "1235",
        "distance": 0,
        "mobile": "0124484866",
        "activation_status": "Active",
        "created_at": "2020-07-31 08:29:42"
    }
]

1 个答案:

答案 0 :(得分:1)

您的CustomerAddress的构造函数是错误的。应该是:

  CustomerAddress({
    this.id,
    this.customerId,
    this.addressType,
    this.address1,
    this.address2,
    this.postcode,
    this.distance,
    this.mobile,
    this.activationStatus,
    this.createdAt,
  });

在您的示例中,它仅接受参数,但只会丢弃值。