【问题标题】:How to pass List of Custom Objects using Dart's http?如何使用 Dart 的 http 传递自定义对象列表?
【发布时间】:2019-11-08 10:13:28
【问题描述】:

我正在尝试将自定义对象列表传递给我的 API。

以下是我的代码

import 'package:meta/meta.dart';
import 'package:json_annotation/json_annotation.dart';

part 'submit_survey_question_options_model.g.dart';

@JsonSerializable(nullable: false)
class SubmitSurveyQuestionOptionsModel {
  String questionId;
  String answer;

  SubmitSurveyQuestionOptionsModel(
      {@required this.questionId, @required this.answer});

  factory SubmitSurveyQuestionOptionsModel.fromJson(
          Map<String, dynamic> json) =>
      _$SubmitSurveyQuestionOptionsModelFromJson(json);

  Map<String, dynamic> toJson() =>
      _$SubmitSurveyQuestionOptionsModelToJson(this);
}



Future<SubmitSurveyModel> submitSurvey(
      String userId,
      String facultyId,
      String surveyId,
      List<SubmitSurveyQuestionOptionsModel> submitSurveyQuestionOptionList,
      String subjectId) async {
    Map<String, dynamic> body = {
      "userId": userId,
      "facultyId": facultyId,
      "surveyId": surveyId,
      "survey": submitSurveyQuestionOptionList,
      "subjectId": subjectId
    };

    final response = await http.post(
      SUBMIT_SURVEY_URL,
      body: json.encode(body),
    );

    SubmitSurveyModel submitSurveyModel = standardSerializers.deserializeWith(
        SubmitSurveyModel.serializer, json.decode(response.body));


    return submitSurveyModel;
  }

我在我的pubspec 中使用json_serializable。 我使用flutter packages pub run build_runner build 构建了可序列化的类

由于数据未正确提交,我无法弄清楚是什么问题?

我已经参考了以下链接,但无法正常工作

Flutter Error: type 'AddressInfo' is not a subtype of type 'String' in type cast

Dart Error Converting Object To JSON

【问题讨论】:

  • 您能否详细说明“数据未正确提交”是什么意思?另外,您的 API 期望是什么?
  • @jdixon04 submitSurveyQuestionOptionList 的数据没有反映在服务器上,尽管它在提交时有数据。我也没有从服务器 api 收到错误。
  • 您是否尝试过从 Flutter 捕获网络请求,然后通过 Postman 或 Insomnia 针对您的 API 提交?抱歉,我想帮忙,但没有足够的详细信息。
  • @jdixon04 我尝试通过邮递员提交并正确提交

标签: http flutter dart


【解决方案1】:

尝试传递以下标题

  Map<String, String> headers = {
      'Content-type': 'application/json',
      'Accept': 'application/json',
    };

  final response = await http.post(Uri.encodeFull(url), body: json.encode(body), headers: headers);

【讨论】:

    【解决方案2】:
    1. 您需要在正文中添加json.encode(body)
    2. 添加这两个标题
      'Content-type': 'application/json',
      '接受':'应用程序/json',

       http.post(url,<br>
          body: json.encode(body),
          headers: { 'Content-type': 'application/json',
            'Accept': 'application/json'},
          encoding: encoding)
          .then((http.Response response) {
      // print(response.toString());
          }
      

    【讨论】:

      【解决方案3】:

      你可以这样做:

      List<String> comments = ["1", "2", "3"];
      Map<String, dynamic> args = {"comments": comments};
      String url = "test.com";
      var body = json.encode(args);
      final response = await http
          .post(url, body: body, headers: {'Content-type': 'application/json'});
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-12-30
        • 2018-10-25
        • 2021-09-24
        • 1970-01-01
        • 2018-02-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多