Talha Javed Khan
Talha Javed Khan

Reputation: 420

Parsing JSON (List of Maps) returns null

I have made model classes to parse JSON. I am trying to parse a List of maps. But for some reason, it is returning null. When I print API response, it is fine When I print decoded API response, it is also fine When I print value from decoded Map, it is also fine But when I do using the classes, it is not working.

Can somebody help, Why is this happening?

Model

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 CALL

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());
      }

These work perfectly and print the data

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

BUT These print null

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

CONSOLE LOG

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 RESPONSE

[
    {
        "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"
    }
]

Upvotes: 0

Views: 140

Answers (1)

julemand101
julemand101

Reputation: 31309

Your constructor for CustomerAddress is wrong. It should be:

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

In your example it just takes the arguments but will just throw the values away.

Upvotes: 1

Related Questions