skip to Main Content

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


  1.  photo = json['photo'] ?? "";
    

    For "target": null, do the same . your problem will be solved

    Login or Signup to reply.
  2. In TaskDetailsModel, you are doing jsonDecode again for the json/map object.

    Try replace first code block with..

    List<CheckListModel>? checklists;
    TaskDetailsModel.fromJson(Map<String, dynamic> json) {
            checklists = (json['checklists'] as List)
                .map((dynamic item) => CheckListModel.fromJson(item))
                .toList();
          }
    

    Edited

    And the second block(CheckListModel) should be like..

    List<CheckListResultItemsModel>? checklistResultItems;
    CheckListModel.fromJson(Map<String, dynamic> json) {
            //checklistResultItems = json['checklistResultItems'];//remove this line
            if (json['checklistResultItems'] != null) {
              checklistResultItems = <CheckListResultItemsModel>[];
              json['checklistResultItems'].forEach((v) {
                checklistResultItems?.add(CheckListResultItemsModel.fromJson(v));
              });
            }
          }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search