''InternalLinkedHashmap not a subtype' error when parsing Json Flutter
I am using TypeAheadFormField to show suggestions to the user as they type, with data pulled from an API designed to autosuggest items.
I have isolated the issue and it is failing at the suggestionsCallback: stage, as the call is made. I have isolated it to the API call and the parsing of the data. I have tried lots of variations in the Class, call etc but can't get the error to go on running.
The error showing under text field(on simulator)
My Class (Made with Quicktype.io) I have tried lots of versions and none worked for me.
Items itemsFromJson(String str) => Items.fromJson(json.decode(str));
String itemsToJson(Items data) => json.encode(data.toJson());
class Items {
Items({
required this.items,
required this.queryTerms,
});
List<Item>? items;
List<dynamic>? queryTerms;
factory Items.fromJson(Map<String, dynamic> json) => Items(
items: json["items"] == null ? null : List<Item>.from(json["items"].map((x) => Item.fromJson(x))),
queryTerms: json["queryTerms"] == null ? null : List<dynamic>.from(json["queryTerms"].map((x) => x)),
);
Map<String, dynamic> toJson() => {
"items": items == null ? null : List<dynamic>.from(items!.map((x) => x.toJson())),
"queryTerms": queryTerms == null ? null : List<dynamic>.from(queryTerms!.map((x) => x)),
};
}
class Item {
Item({
required this.title,
required this.id,
required this.resultType,
required this.address,
required this.position,
required this.access,
required this.distance,
required this.categories,
required this.references,
required this.highlights,
required this.localityType,
required this.mapView,
required this.administrativeAreaType,
});
String? title;
String? id;
ResultType? resultType;
ItemAddress? address;
Position? position;
List<Position>? access;
int? distance;
List<Category>? categories;
List<Reference>? references;
Highlights? highlights;
String? localityType;
MapView? mapView;
String? administrativeAreaType;
factory Item.fromJson(Map<String, dynamic> json) => Item(
title: json["title"] == null ? null : json["title"],
id: json["id"] == null ? null : json["id"],
resultType: json["resultType"] == null ? null : resultTypeValues.map![json["resultType"]],
address: json["address"] == null ? null : ItemAddress.fromJson(json["address"]),
position: json["position"] == null ? null : Position.fromJson(json["position"]),
access: json["access"] == null ? null : List<Position>.from(json["access"].map((x) => Position.fromJson(x))),
distance: json["distance"] == null ? null : json["distance"],
categories: json["categories"] == null ? null : List<Category>.from(json["categories"].map((x) => Category.fromJson(x))),
references: json["references"] == null ? null : List<Reference>.from(json["references"].map((x) => Reference.fromJson(x))),
highlights: json["highlights"] == null ? null : Highlights.fromJson(json["highlights"]),
localityType: json["localityType"] == null ? null : json["localityType"],
mapView: json["mapView"] == null ? null : MapView.fromJson(json["mapView"]),
administrativeAreaType: json["administrativeAreaType"] == null ? null : json["administrativeAreaType"],
);
Map<String, dynamic> toJson() => {
"title": title == null ? null : title,
"id": id == null ? null : id,
"resultType": resultType == null ? null : resultTypeValues.reverse![resultType],
"address": address == null ? null : address!.toJson(),
"position": position == null ? null : position!.toJson(),
"access": access == null ? null : List<dynamic>.from(access!.map((x) => x.toJson())),
"distance": distance == null ? null : distance,
"categories": categories == null ? null : List<dynamic>.from(categories!.map((x) => x.toJson())),
"references": references == null ? null : List<dynamic>.from(references!.map((x) => x.toJson())),
"highlights": highlights == null ? null : highlights!.toJson(),
"localityType": localityType == null ? null : localityType,
"mapView": mapView == null ? null : mapView!.toJson(),
"administrativeAreaType": administrativeAreaType == null ? null : administrativeAreaType,
};
}
class Position {
Position({
required this.lat,
required this.lng,
});
double? lat;
double? lng;
factory Position.fromJson(Map<String, dynamic> json) => Position(
lat: json["lat"] == null ? null : json["lat"].toDouble(),
lng: json["lng"] == null ? null : json["lng"].toDouble(),
);
Map<String, dynamic> toJson() => {
"lat": lat == null ? null : lat,
"lng": lng == null ? null : lng,
};
}
class ItemAddress {
ItemAddress({
required this.label,
});
String? label;
factory ItemAddress.fromJson(Map<String, dynamic> json) => ItemAddress(
label: json["label"] == null ? null : json["label"],
);
Map<String, dynamic> toJson() => {
"label": label == null ? null : label,
};
}
class Category {
Category({
required this.id,
required this.name,
required this.primary,
});
String? id;
String? name;
bool? primary;
factory Category.fromJson(Map<String, dynamic> json) => Category(
id: json["id"] == null ? null : json["id"],
name: json["name"] == null ? null : json["name"],
primary: json["primary"] == null ? null : json["primary"],
);
Map<String, dynamic> toJson() => {
"id": id == null ? null : id,
"name": name == null ? null : name,
"primary": primary == null ? null : primary,
};
}
class Highlights {
Highlights({
required this.title,
required this.address,
});
List<Title>? title;
HighlightsAddress? address;
factory Highlights.fromJson(Map<String, dynamic> json) => Highlights(
title: json["title"] == null ? null : List<Title>.from(json["title"].map((x) => Title.fromJson(x))),
address: json["address"] == null ? null : HighlightsAddress.fromJson(json["address"]),
);
Map<String, dynamic> toJson() => {
"title": title == null ? null : List<dynamic>.from(title!.map((x) => x.toJson())),
"address": address == null ? null : address!.toJson(),
};
}
class HighlightsAddress {
HighlightsAddress({
required this.label,
});
List<Title>? label;
factory HighlightsAddress.fromJson(Map<String, dynamic> json) => HighlightsAddress(
label: json["label"] == null ? null : List<Title>.from(json["label"].map((x) => Title.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"label": label == null ? null : List<dynamic>.from(label!.map((x) => x.toJson())),
};
}
class Title {
Title({
required this.start,
required this.end,
});
int? start;
int? end;
factory Title.fromJson(Map<String, dynamic> json) => Title(
start: json["start"] == null ? null : json["start"],
end: json["end"] == null ? null : json["end"],
);
Map<String, dynamic> toJson() => {
"start": start == null ? null : start,
"end": end == null ? null : end,
};
}
class MapView {
MapView({
required this.west,
required this.south,
required this.east,
required this.north,
});
double? west;
double? south;
double? east;
double? north;
factory MapView.fromJson(Map<String, dynamic> json) => MapView(
west: json["west"] == null ? null : json["west"].toDouble(),
south: json["south"] == null ? null : json["south"].toDouble(),
east: json["east"] == null ? null : json["east"].toDouble(),
north: json["north"] == null ? null : json["north"].toDouble(),
);
Map<String, dynamic> toJson() => {
"west": west == null ? null : west,
"south": south == null ? null : south,
"east": east == null ? null : east,
"north": north == null ? null : north,
};
}
class Reference {
Reference({
required this.supplier,
required this.id,
});
Supplier? supplier;
String? id;
factory Reference.fromJson(Map<String, dynamic> json) => Reference(
supplier: json["supplier"] == null ? null : Supplier.fromJson(json["supplier"]),
id: json["id"] == null ? null : json["id"],
);
Map<String, dynamic> toJson() => {
"supplier": supplier == null ? null : supplier!.toJson(),
"id": id == null ? null : id,
};
}
class Supplier {
Supplier({
required this.id,
});
String id;
factory Supplier.fromJson(Map<String, dynamic> json) => Supplier(
id: json["id"] == null ? null : json["id"],
);
Map<String, dynamic> toJson() => {
"id": id == null ? null : id,
};
}
enum ResultType { PLACE, LOCALITY, ADMINISTRATIVE_AREA, STREET }
final resultTypeValues = EnumValues({
"administrativeArea": ResultType.ADMINISTRATIVE_AREA,
"locality": ResultType.LOCALITY,
"place": ResultType.PLACE,
"street": ResultType.STREET
});
class EnumValues<T> {
Map<String, T>? map;
Map<T, String>? reverseMap;
EnumValues(this.map);
Map<T, String>? get reverse {
if (reverseMap == null) {
reverseMap = map!.map((k, v) => new MapEntry(v, k));
}
return reverseMap;
}
}
My API call for the data
class UserApi {
static FutureOr<Iterable<Item>> getUserSuggestions(String query) async {
final url = Uri.parse('https_call');
final response = await http.get(url);
if (response.statusCode == 200) {
final List users = json.decode(response.body);
return users.map((json) => Item.fromJson(json)).where((user) {
final nameLower = user.address!.label!.toLowerCase();
final queryLower = query.toLowerCase();
return nameLower.contains(queryLower);
}).toList();
} else {
throw Exception();
}
}
}
Thank you
Solution 1:[1]
Solution was to put
if (response.statusCode == 200) {
Map<String, dynamic> users1 = json.decode(response.body);
List<dynamic> users = users1["items"];
in place of
if (response.statusCode == 200) {
final List users = json.decode(response.body);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | al246 |

