0

i am trying to send nested data to API using the HTTP post method. but I am not able to send the data due to a mapping error. below is my model class that I have made to get the data from API-

class Assessment {
  Assessment({
    this.id,
    this.subId,
    this.question,
    this.assessment,
  });

  String id;
  String subId;
  String question;
  List<Assessment> assessment;

  factory AssessmentQuestionList.fromJson(Map<String, dynamic> json) =>
      AssessmentQuestionList(
        id: json["id"],
        subCategoryId: json["subId"],
        question: json["question"],
        assessment: List<Assessment>.from(
            json["assessment"]
                .map((x) => Assessment.fromJson(x))),
      );

  Map<String, dynamic> toJson() => {
        "id": id,
        "assessmentId": assessmentId,
        "subCategoryId": subCategoryId,
        "question": question,
        "assessment":
            List<dynamic>.from(assessment.map((x) => x.toJson())),
      };
}

class Assessment {
  Assessment({
    this.id,
    this.assessmentQuestionId,
    this.option,
    this.isChecked,
  });

  String id;
  String assessmentQuestionId;
  dynamic option;
  bool isChecked;

  factory AssessmentAnswerList.fromJson(Map<String, dynamic> json) =>
      AssessmentAnswerList(
        id: json["id"],
        assessmentQuestionId: json["assessmentQuestionId"],
        option: json["option"],
        //optionValues.map[json["option"]],
        isChecked: json["isChecked"],
      );

  Map<String, dynamic> toJson() => {
        "id": id,
        "assessmentQuestionId": assessmentQuestionId,
        "option": option, //optionValues.reverse[option],
        "isChecked": isChecked,
      };
}

after making list and adding data dynamically into this, I am sending the data to my future method that is to be used for api calls as below -

  Future<http.Response> saveassess(String authToken, String id, String assessmentId,
      var QuestionList, String childUserId,
      String notes) async {

    String uriparts =apiEndPoint.getUriParts('assess/Assess');
    Uri Url = apiEndPoint.getHTTPUri(uriparts);

    final response = await http.post(
      Url,
      headers: <String, String>{
        'Content-Type': 'application/json; charset=UTF-8',
        'Authorization': authToken
      },
      body: jsonEncode(<String, dynamic>{
          "id": id,
          "assessmentId": assessmentId,
          "assessmentQuestionAnswerList": QuestionList,
          "childUserId": childUserId,
          "notes": notes,
      }),
    );
    print(response.body);
    handleAssesmentsResponse(response);
  }

can anyone tell me what I am doing wrong as this is the first time I am working with nested API calls? Thank you

1 Answer 1

1

you also have to encode each individual list like below,

jsonEncode(<String, dynamic>{
          "id": id,
          "assessmentId": assessmentId,
          "assessmentQuestionAnswerList": jsonEncode(QuestionList),
          "assessmentQuestionAnswerList.assessmentAnswerList": jsonEncode(AnswerList),
          "childUserId": childUserId,
          "notes": notes,
      }),
Sign up to request clarification or add additional context in comments.

2 Comments

i did this but i am getting error - I/flutter ( 2723): failed in details Converting object to an encodable object failed: LinkedHashSet len:8
may be you need to specify data type for QuestionList to encode, because it is not a datatype that is not predefined in dart

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.