gpt4 book ai didi

http - 如何使用 Dart 的 http 传递自定义对象列表?

转载 作者:IT王子 更新时间:2023-10-29 06:39:14 26 4
gpt4 key购买 nike

我正在尝试将自定义对象列表传递给我的 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

最佳答案

尝试传递以下 header

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

关于http - 如何使用 Dart 的 http 传递自定义对象列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56773255/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com