Here is my controller code, why is data returning as a null value?
Future<List<GetEntityKindsModel>> getEntityKinds() async
{
List<GetEntityKindsModel> data = [];
try
{
dynamic response = await http.get(Uri.parse(url));
dynamic jsonData = jsonDecode(response.body);
data = jsonData.map((item)=>GetEntityKindsModel.fromJson(item));
print('Data -- $data');
}
catch(e)
{
print('Error -- $e');
}
return data;
}
I tried typecasting both jsonData and data into list format but still data returned as null. Is there any issue my with mapping of model to controller? Model has 2 nested classes
class GetEntityKindsModel {
GetEntityKindsModel({
required this.name,
required this.type,
required this.classType,
required this.label,
required this.pluralLabel,
this.sharedVisibility,
this.subscribe,
required this.id,
this.entityName,
required this.parentEntityId,
required this.languageId,
this.timestamp,
this.tagText,
required this.entityIndex,
this.code,
this.blobKey,
});
String name;
Type type;
ClassType classType;
String label;
String pluralLabel;
String? sharedVisibility;
Subscribe? subscribe;
int id;
String? entityName;
int parentEntityId;
LanguageId languageId;
int? timestamp;
String? tagText;
List<String> entityIndex;
Code? code;
BlobKey? blobKey;
factory GetEntityKindsModel.fromJson(Map<String, dynamic> json) => GetEntityKindsModel(
name: json["name"],
type: typeValues.map[json["type"]]!,
classType: classTypeValues.map[json["classType"]]!,
label: json["label"],
pluralLabel: json["pluralLabel"],
sharedVisibility: json["sharedVisibility"],
subscribe: subscribeValues.map[json["subscribe"]]!,
id: json["id"],
entityName: json["entityName"],
parentEntityId: json["parentEntityId"],
languageId: languageIdValues.map[json["languageId"]]!,
timestamp: json["timestamp"],
tagText: json["tagText"],
entityIndex: List<String>.from(json["entityIndex"].map((x) => x)),
code: json["code"] == null ? null : Code.fromJson(json["code"]),
blobKey: json["blobKey"] == null ? null : BlobKey.fromJson(json["blobKey"]),
);
Map<String, dynamic> toJson() => {
"name": name,
"type": typeValues.reverse[type],
"classType": classTypeValues.reverse[classType],
"label": label,
"pluralLabel": pluralLabel,
"sharedVisibility": sharedVisibility,
"subscribe": subscribeValues.reverse[subscribe],
"id": id,
"entityName": entityName,
"parentEntityId": parentEntityId,
"languageId": languageIdValues.reverse[languageId],
"timestamp": timestamp,
"tagText": tagText,
"entityIndex": List<dynamic>.from(entityIndex.map((x) => x)),
"code": code?.toJson(),
"blobKey": blobKey?.toJson(),
};
}
class BlobKey {
BlobKey({
required this.blobKey,
});
String blobKey;
factory BlobKey.fromJson(Map<String, dynamic> json) => BlobKey(
blobKey: json["blobKey"],
);
Map<String, dynamic> toJson() => {
"blobKey": blobKey,
};
}
enum ClassType { STATIC, NULL, DYNAMIC }
final classTypeValues = EnumValues({
"dynamic": ClassType.DYNAMIC,
"null": ClassType.NULL,
"static": ClassType.STATIC
});
class Code {
Code({
required this.value,
});
String value;
factory Code.fromJson(Map<String, dynamic> json) => Code(
value: json["value"],
);
Map<String, dynamic> toJson() => {
"value": value,
};
}
enum LanguageId { EN_US }
final languageIdValues = EnumValues({
"en_US": LanguageId.EN_US
});
enum Subscribe { YES, EMPTY }
final subscribeValues = EnumValues({
"": Subscribe.EMPTY,
"yes": Subscribe.YES
});
enum Type { PRIMARY, RELATED }
final typeValues = EnumValues({
"PRIMARY": Type.PRIMARY,
"RELATED": Type.RELATED
});
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;
}
}
Model class has been made from app.quicktype.io based on the json response of the api, which is too long so can't post here.
The error you are getting is
Error -- Null check operator used on a null value
It would have been useful if you mentioned that before..
It simply means that you are using !
on variable that is null
. In your code there is 4 places where this can happen:
type: typeValues.map[json["type"]]!,
classType: classTypeValues.map[json["classType"]]!,
subscribe: subscribeValues.map[json["subscribe"]]!,
languageId: languageIdValues.map[json["languageId"]]!,
I'd suggest to remove the !
for all 4 and make the corresponding field nullable, like
Type? type;
ClassType? classType;
Subscribe? subscribe;
LanguageId? languageId;
Subscribe is already like this.