【问题标题】:post nested array data to api in flutter在flutter中将嵌套数组数据发布到api
【发布时间】:2022-01-04 06:51:56
【问题描述】:

我正在尝试使用 HTTP post 方法将嵌套数据发送到 API。 但由于映射错误,我无法发送数据。 下面是我为从 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,
      };
}

在创建列表并将数据动态添加到其中之后,我将数据发送到我未来的方法,该方法将用于以下 api 调用 -

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

谁能告诉我我做错了什么,因为这是我第一次使用嵌套 API 调用? 谢谢

【问题讨论】:

    标签: json flutter api http flutter-dependencies


    【解决方案1】:

    您还必须对每个单独的列表进行编码,如下所示,

    jsonEncode(<String, dynamic>{
              "id": id,
              "assessmentId": assessmentId,
              "assessmentQuestionAnswerList": jsonEncode(QuestionList),
              "assessmentQuestionAnswerList.assessmentAnswerList": jsonEncode(AnswerList),
              "childUserId": childUserId,
              "notes": notes,
          }),
    

    【讨论】:

    • 我这样做了,但我得到了错误 - I/flutter (2723): failed in details 将对象转换为可编码对象失败:LinkedHashSet len:8
    • 可能需要为 QuestionList 指定数据类型进行编码,因为它不是 dart 中未预定义的数据类型
    猜你喜欢
    • 2021-12-16
    • 2022-01-01
    • 1970-01-01
    • 2015-10-13
    • 2021-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-14
    相关资源
    最近更新 更多