I want to retrieve a list and a nested list which is checklists[] and checklistResultItems[], how can I do that?
{
"target": null,
"checklists": [ //this list
{
"roomID": 1078,
"completedTasks": 2,
"checklistResultItems": [ //and this list
{
"photo": null
},
]
}
]
}
This is my TaskDetailsModel
List<dynamic>? checklists;
TaskDetailsModel.fromJson(Map<String, dynamic> json) {
checklists = jsonDecode(json['checklists'])
.map((dynamic item) => CheckListModel.fromJson(item))
.toList();
}
This is my CheckListModel
List<CheckListResultItemsModel>? checklistResultItems;
CheckListModel.fromJson(Map<String, dynamic> json) {
checklistResultItems = json['checklistResultItems'];
if (json['checklistResultItems'] != null) {
checklistResultItems = <CheckListResultItemsModel>[];
json['checklistResultItems'].forEach((v) {
checklistResultItems?.add(CheckListResultItemsModel.fromJson(v));
});
}
}
This is my CheckListResultItems model
CheckListResultItemsModel.fromJson(Map<String, dynamic> json) {
checklistResultItemID = json['checklistResultItemID'];
checklistItemID = json['checklistItemID'];
checklistItemName = json['checklistItemName'];
checklistItemInfo = json['checklistItemInfo'];
checkListResultID = json['checkListResultID'];
checked = json['checked'];
unableToComplete = json['unableToComplete'];
comments = json['comments'];
photo = json['photo'];
}
How I retrieve my data from API, I am currently getting this error type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String'
.
getTaskDetailsApi(int businessID) async {
try {
String url = ApiEndpoint.getTaskDetailsByBusinessID
.replaceAll("{id}", businessID.toString());
CommonResponse response = await apiClient.get(url);
if (response.success! && response.fullResponse != null) {
final responseData = response.fullResponse.data;
List<dynamic> body = jsonDecode(responseData);
List<TaskDetailsModel> taskDetails = body
.map((dynamic item) => TaskDetailsModel.fromJson(item))
.toList();
return taskDetails;
} else {
Utils.showErrorSnackBar(response.message);
}
} catch (e) {
Utils.showErrorSnackBar(e.toString());
}
}
2
Answers
For "target": null, do the same . your problem will be solved
In
TaskDetailsModel
, you are doingjsonDecode
again for thejson/map
object.Try replace first code block with..
Edited
And the second block(
CheckListModel
) should be like..