我有一个对象,当我尝试此操作时想通过dio发送到api,但出现未处理的异常:DioError [DioErrorType.DEFAULT]:将对象转换为可编码的对象失败。我也尝试将对象转换为Json,但仍然遇到另一个错误,下面将对此进行介绍。
static Future<DiagnosisModel> doDiagnosis(String url, String id,String choiceId, bool initial) async{
Dio dio = new Dio();
dio.options.headers['content-Type'] = 'application/json';
dio.options.headers['App-Id'] = ChatBotApi.applicationId;
dio.options.headers['App-Key'] = ChatBotApi.applicationKey;
Response response;
DiagnosisSendingModel diagnosisSendingModel;
Evidence evidence;
if(initial){
evidence = Evidence();
diagnosisSendingModel = DiagnosisSendingModel();
diagnosisSendingModel.evidence = List<Evidence>();
evidence.id = id;
evidence.initial = initial;
diagnosisSendingModel.sex = "male";
diagnosisSendingModel.age = 23;
diagnosisSendingModel.evidence.add(evidence);
response = await dio.post(
url,
data: diagnosisSendingModel
);
}else{
evidence = Evidence();
diagnosisSendingModel = DiagnosisSendingModel();
evidence.id = id;
if(choiceId == "present"){
evidence.choiceId = ChoiceId.PRESENT;
}else{
evidence.choiceId = ChoiceId.ABSENT;
}
diagnosisSendingModel.sex = "male";
diagnosisSendingModel.age = 23;
diagnosisSendingModel.evidence.add(evidence);
response = await dio.post(
url,
data: diagnosisSendingModel
);
}
DiagnosisModel diagnosisModel;
if(response.statusCode == 200){
diagnosisModel = DiagnosisModel.fromJson(response.data);
print(diagnosisModel);
}else{
throw("Error ${response.statusCode}");
}
return diagnosisModel;
}
我也尝试过转换为json,但仍然出现错误
Unhandled Exception: Converting object to an encodable object failed: Instance of 'DiagnosisSendingModel'
下面的代码
static Future<DiagnosisModel> doDiagnosis(String url, String id,String choiceId, bool initial) async{
Dio dio = new Dio();
dio.options.headers['content-Type'] = 'application/json';
dio.options.headers['App-Id'] = ChatBotApi.applicationId;
dio.options.headers['App-Key'] = ChatBotApi.applicationKey;
Response response;
DiagnosisSendingModel diagnosisSendingModel;
Evidence evidence;
if(initial){
evidence = Evidence();
diagnosisSendingModel = DiagnosisSendingModel();
diagnosisSendingModel.evidence = List<Evidence>();
evidence.id = id;
evidence.initial = initial;
diagnosisSendingModel.sex = "male";
diagnosisSendingModel.age = 23;
diagnosisSendingModel.evidence.add(evidence);
var jsonVar = json.encode(diagnosisSendingModel);
response = await dio.post(
url,
data: jsonVar
);
}else{
evidence = Evidence();
diagnosisSendingModel = DiagnosisSendingModel();
evidence.id = id;
if(choiceId == "present"){
evidence.choiceId = ChoiceId.PRESENT;
}else{
evidence.choiceId = ChoiceId.ABSENT;
}
diagnosisSendingModel.sex = "male";
diagnosisSendingModel.age = 23;
diagnosisSendingModel.evidence.add(evidence);
var jsonVar = json.encode(diagnosisSendingModel);
response = await dio.post(
url,
data: jsonVar
);
}
DiagnosisModel diagnosisModel;
if(response.statusCode == 200){
diagnosisModel = DiagnosisModel.fromJson(response.data);
print(diagnosisModel);
}else{
throw("Error ${response.statusCode}");
}
return diagnosisModel;
}
下面是我的模特
String sex;
int age;
List<Evidence> evidence;
ExtrasSending extras;
DiagnosisSendingModel({
this.sex,
this.age,
this.evidence,
this.extras,
});
factory DiagnosisSendingModel.fromJson(Map<String, dynamic> json) => DiagnosisSendingModel(
sex: json["sex"],
age: json["age"],
evidence: List<Evidence>.from(json["evidence"].map((x) => Evidence.fromJson(x))),
extras: ExtrasSending.fromJson(json["extras"]),
);
Map<String, dynamic> toJson() => {
"sex": sex,
"age": age,
"evidence": List<dynamic>.from(evidence.map((x) => x.toJson())),
"extras": extras.toJson(),
};
}
class Evidence {
String id;
ChoiceId choiceId;
bool initial;
Evidence({
this.id,
this.choiceId,
this.initial,
});
factory Evidence.fromJson(Map<String, dynamic> json) => Evidence(
id: json["id"],
choiceId: choiceIdValues.map[json["choice_id"]],
initial: json["initial"] == null ? null : json["initial"],
);
Map<String, dynamic> toJson() => {
"id": id,
"choice_id": choiceIdValues.reverse[choiceId],
"initial": initial == null ? null : initial,
};
}
enum ChoiceId { PRESENT, ABSENT }
final choiceIdValues = EnumValues({
"absent": ChoiceId.ABSENT,
"present": ChoiceId.PRESENT
});
class ExtrasSending {
bool enableTriage5;
ExtrasSending({
this.enableTriage5,
});
factory ExtrasSending.fromJson(Map<String, dynamic> json) => ExtrasSending(
enableTriage5: json["enable_triage_5"],
);
Map<String, dynamic> toJson() => {
"enable_triage_5": enableTriage5,
};
}
class EnumValues<T> {
Map<String, T> map;
Map<T, String> reverseMap;
EnumValues(this.map);
Map<T, String> get reverse {
if (reverseMap == null) {
reverseMap = map.map((k, v) => new MapEntry(v, k));
}
return reverseMap;
}
} ```
答案 0 :(得分:0)
替换:
var jsonVar = json.encode(diagnosisSendingModel);
与此:
var jsonVar = json.encode(diagnosisSendingModel.toJson());
答案 1 :(得分:0)
试试这个
C:\Users\<USER_NAME>\AppData\Local\Android\Sdk\platform-tools
adb reverse tcp:{portOfServer} tcp:{portOfServer}
例如:adb reverse tcp:8081 tcp:8081