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