I am learning to work with JSON. This is my first time doing this.
I get the JSON from an API call with this code:
if (response.statusCode == 200) {
var json = response.body;
// return itemsFromJson(json);
getDataFromApiFromJson(json);
}
Here is the code for getDataFromApiFromJson()
. This code was generated by QuickType.com JSON to Dart:
import 'dart:convert';
GetPropertyDataFromApi getDataFromApiFromJson(String str) => GetPropertyDataFromApi.fromJson(json.decode(str));
String getDataFromApiToJson(GetPropertyDataFromApi data) => json.encode(data.toJson());
class GetPropertyDataFromApi {
String type;
Items items;
GetPropertyDataFromApi({
required this.type,
required this.items,
});
factory GetPropertyDataFromApi.fromJson(Map<String, dynamic> json) => GetPropertyDataFromApi(
type: json["type"],
items: Items.fromJson(json["items"]),
);
Map<String, dynamic> toJson() => {
"type": type,
"items": items.toJson(),
};
}
class Items {
String type;
ItemsProperties properties;
Items({
required this.type,
required this.properties,
});
factory Items.fromJson(Map<String, dynamic> json) => Items(
type: json["type"],
properties: ItemsProperties.fromJson(json["properties"]),
);
Map<String, dynamic> toJson() => {
"type": type,
"properties": properties.toJson(),
};
}
class ItemsProperties {
AddressLine1 addressLine1;
AddressLine1 city;
AddressLine1 state;
AddressLine1 zipCode;
AddressLine1 formattedAddress;
AddressLine1 assessorId;
AddressLine1 bedrooms;
AddressLine1 county;
AddressLine1 legalDescription;
AddressLine1 squareFootage;
AddressLine1 subdivision;
AddressLine1 yearBuilt;
AddressLine1 bathrooms;
AddressLine1 lotSize;
AddressLine1 propertyType;
AddressLine1 lastSaleDate;
Features features;
TaxAssessment taxAssessment;
PropertyTaxes propertyTaxes;
Owner owner;
AddressLine1 id;
AddressLine1 longitude;
AddressLine1 latitude;
ItemsProperties({
required this.addressLine1,
required this.city,
required this.state,
required this.zipCode,
required this.formattedAddress,
required this.assessorId,
required this.bedrooms,
required this.county,
required this.legalDescription,
required this.squareFootage,
required this.subdivision,
required this.yearBuilt,
required this.bathrooms,
required this.lotSize,
required this.propertyType,
required this.lastSaleDate,
required this.features,
required this.taxAssessment,
required this.propertyTaxes,
required this.owner,
required this.id,
required this.longitude,
required this.latitude,
});
factory ItemsProperties.fromJson(Map<String, dynamic> json) => ItemsProperties(
addressLine1: AddressLine1.fromJson(json["addressLine1"]),
city: AddressLine1.fromJson(json["city"]),
state: AddressLine1.fromJson(json["state"]),
zipCode: AddressLine1.fromJson(json["zipCode"]),
formattedAddress: AddressLine1.fromJson(json["formattedAddress"]),
assessorId: AddressLine1.fromJson(json["assessorID"]),
bedrooms: AddressLine1.fromJson(json["bedrooms"]),
county: AddressLine1.fromJson(json["county"]),
legalDescription: AddressLine1.fromJson(json["legalDescription"]),
squareFootage: AddressLine1.fromJson(json["squareFootage"]),
subdivision: AddressLine1.fromJson(json["subdivision"]),
yearBuilt: AddressLine1.fromJson(json["yearBuilt"]),
bathrooms: AddressLine1.fromJson(json["bathrooms"]),
lotSize: AddressLine1.fromJson(json["lotSize"]),
propertyType: AddressLine1.fromJson(json["propertyType"]),
lastSaleDate: AddressLine1.fromJson(json["lastSaleDate"]),
features: Features.fromJson(json["features"]),
taxAssessment: TaxAssessment.fromJson(json["taxAssessment"]),
propertyTaxes: PropertyTaxes.fromJson(json["propertyTaxes"]),
owner: Owner.fromJson(json["owner"]),
id: AddressLine1.fromJson(json["id"]),
longitude: AddressLine1.fromJson(json["longitude"]),
latitude: AddressLine1.fromJson(json["latitude"]),
);
Map<String, dynamic> toJson() => {
"addressLine1": addressLine1.toJson(),
"city": city.toJson(),
"state": state.toJson(),
"zipCode": zipCode.toJson(),
"formattedAddress": formattedAddress.toJson(),
"assessorID": assessorId.toJson(),
"bedrooms": bedrooms.toJson(),
"county": county.toJson(),
"legalDescription": legalDescription.toJson(),
"squareFootage": squareFootage.toJson(),
"subdivision": subdivision.toJson(),
"yearBuilt": yearBuilt.toJson(),
"bathrooms": bathrooms.toJson(),
"lotSize": lotSize.toJson(),
"propertyType": propertyType.toJson(),
"lastSaleDate": lastSaleDate.toJson(),
"features": features.toJson(),
"taxAssessment": taxAssessment.toJson(),
"propertyTaxes": propertyTaxes.toJson(),
"owner": owner.toJson(),
"id": id.toJson(),
"longitude": longitude.toJson(),
"latitude": latitude.toJson(),
};
}
class AddressLine1 {
Type type;
AddressLine1({
required this.type,
});
factory AddressLine1.fromJson(Map<String, dynamic> json) => AddressLine1(
type: typeValues.map[json["type"]]!,
);
Map<String, dynamic> toJson() => {
"type": typeValues.reverse[type],
};
}
enum Type {
BOOLEAN,
INTEGER,
NUMBER,
STRING
}
final typeValues = EnumValues({
"boolean": Type.BOOLEAN,
"integer": Type.INTEGER,
"number": Type.NUMBER,
"string": Type.STRING
});
class Features {
String type;
Map<String, AddressLine1> properties;
Features({
required this.type,
required this.properties,
});
factory Features.fromJson(Map<String, dynamic> json) => Features(
type: json["type"],
properties: Map.from(json["properties"]).map((k, v) => MapEntry<String, AddressLine1>(k, AddressLine1.fromJson(v))),
);
Map<String, dynamic> toJson() => {
"type": type,
"properties": Map.from(properties).map((k, v) => MapEntry<String, dynamic>(k, v.toJson())),
};
}
class Owner {
String type;
OwnerProperties properties;
Owner({
required this.type,
required this.properties,
});
factory Owner.fromJson(Map<String, dynamic> json) => Owner(
type: json["type"],
properties: OwnerProperties.fromJson(json["properties"]),
);
Map<String, dynamic> toJson() => {
"type": type,
"properties": properties.toJson(),
};
}
class OwnerProperties {
Names names;
MailingAddress mailingAddress;
OwnerProperties({
required this.names,
required this.mailingAddress,
});
factory OwnerProperties.fromJson(Map<String, dynamic> json) => OwnerProperties(
names: Names.fromJson(json["names"]),
mailingAddress: MailingAddress.fromJson(json["mailingAddress"]),
);
Map<String, dynamic> toJson() => {
"names": names.toJson(),
"mailingAddress": mailingAddress.toJson(),
};
}
class MailingAddress {
String type;
MailingAddressProperties properties;
MailingAddress({
required this.type,
required this.properties,
});
factory MailingAddress.fromJson(Map<String, dynamic> json) => MailingAddress(
type: json["type"],
properties: MailingAddressProperties.fromJson(json["properties"]),
);
Map<String, dynamic> toJson() => {
"type": type,
"properties": properties.toJson(),
};
}
class MailingAddressProperties {
AddressLine1 id;
AddressLine1 addressLine1;
AddressLine1 city;
AddressLine1 state;
AddressLine1 zipCode;
MailingAddressProperties({
required this.id,
required this.addressLine1,
required this.city,
required this.state,
required this.zipCode,
});
factory MailingAddressProperties.fromJson(Map<String, dynamic> json) => MailingAddressProperties(
id: AddressLine1.fromJson(json["id"]),
addressLine1: AddressLine1.fromJson(json["addressLine1"]),
city: AddressLine1.fromJson(json["city"]),
state: AddressLine1.fromJson(json["state"]),
zipCode: AddressLine1.fromJson(json["zipCode"]),
);
Map<String, dynamic> toJson() => {
"id": id.toJson(),
"addressLine1": addressLine1.toJson(),
"city": city.toJson(),
"state": state.toJson(),
"zipCode": zipCode.toJson(),
};
}
class Names {
String type;
AddressLine1 items;
Names({
required this.type,
required this.items,
});
factory Names.fromJson(Map<String, dynamic> json) => Names(
type: json["type"],
items: AddressLine1.fromJson(json["items"]),
);
Map<String, dynamic> toJson() => {
"type": type,
"items": items.toJson(),
};
}
class PropertyTaxes {
String type;
Map<String, PropertyTaxesProperty> properties;
PropertyTaxes({
required this.type,
required this.properties,
});
factory PropertyTaxes.fromJson(Map<String, dynamic> json) => PropertyTaxes(
type: json["type"],
properties: Map.from(json["properties"]).map((k, v) => MapEntry<String, PropertyTaxesProperty>(k, PropertyTaxesProperty.fromJson(v))),
);
Map<String, dynamic> toJson() => {
"type": type,
"properties": Map.from(properties).map((k, v) => MapEntry<String, dynamic>(k, v.toJson())),
};
}
class PropertyTaxesProperty {
String type;
PurpleProperties properties;
PropertyTaxesProperty({
required this.type,
required this.properties,
});
factory PropertyTaxesProperty.fromJson(Map<String, dynamic> json) => PropertyTaxesProperty(
type: json["type"],
properties: PurpleProperties.fromJson(json["properties"]),
);
Map<String, dynamic> toJson() => {
"type": type,
"properties": properties.toJson(),
};
}
class PurpleProperties {
AddressLine1 total;
PurpleProperties({
required this.total,
});
factory PurpleProperties.fromJson(Map<String, dynamic> json) => PurpleProperties(
total: AddressLine1.fromJson(json["total"]),
);
Map<String, dynamic> toJson() => {
"total": total.toJson(),
};
}
class TaxAssessment {
String type;
Map<String, TaxAssessmentProperty> properties;
TaxAssessment({
required this.type,
required this.properties,
});
factory TaxAssessment.fromJson(Map<String, dynamic> json) => TaxAssessment(
type: json["type"],
properties: Map.from(json["properties"]).map((k, v) => MapEntry<String, TaxAssessmentProperty>(k, TaxAssessmentProperty.fromJson(v))),
);
Map<String, dynamic> toJson() => {
"type": type,
"properties": Map.from(properties).map((k, v) => MapEntry<String, dynamic>(k, v.toJson())),
};
}
class TaxAssessmentProperty {
String type;
FluffyProperties properties;
TaxAssessmentProperty({
required this.type,
required this.properties,
});
factory TaxAssessmentProperty.fromJson(Map<String, dynamic> json) => TaxAssessmentProperty(
type: json["type"],
properties: FluffyProperties.fromJson(json["properties"]),
);
Map<String, dynamic> toJson() => {
"type": type,
"properties": properties.toJson(),
};
}
class FluffyProperties {
AddressLine1 value;
AddressLine1 land;
AddressLine1 improvements;
FluffyProperties({
required this.value,
required this.land,
required this.improvements,
});
factory FluffyProperties.fromJson(Map<String, dynamic> json) => FluffyProperties(
value: AddressLine1.fromJson(json["value"]),
land: AddressLine1.fromJson(json["land"]),
improvements: AddressLine1.fromJson(json["improvements"]),
);
Map<String, dynamic> toJson() => {
"value": value.toJson(),
"land": land.toJson(),
"improvements": improvements.toJson(),
};
}
class EnumValues<T> {
Map<String, T> map;
late Map<T, String> reverseMap;
EnumValues(this.map);
Map<T, String> get reverse {
reverseMap = map.map((k, v) => MapEntry(v, k));
return reverseMap;
}
}
I don’t know if I am calling the function correct or not.
This is the error I get when I run the code:
How do I do this in the correct way?
2
Answers
Like pmatatias mentioned it looks like somewhere you are attempting to parse a list of elements as if they were a single element.
You probably need to add a method that converts a json list into a list of elements. For example:
or if you want your function to ignore any elements that can’t be parsed, you could use:
Then any place that supports a list of objects, call this method. For example, does the "features" property return a list of Features elements? If so, then you would want to replace the line that currently says:
with
since I dont know how your json format looks like, I will give example to serialize json to dart object
if your data is not a List, you can directly convert it
you API response are a
List
, even its only 1 items