palak
palak

Reputation: 401

post nested array data to api in flutter

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

Upvotes: 0

Views: 788

Answers (1)

Ruchit
Ruchit

Reputation: 2770

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,
      }),

Upvotes: 1

Related Questions