颤振中的 FormatException
FormatException in flutter
我正在尝试使用 post http 方法将数据发送到我的服务器。后端工作完美(用 postman 进行测试)但是我在 flutter 中遇到了这个错误!
这是我的代码:
Future<MesureModel?> postMeasurement(String description) async {
final response = await http.post(
Uri.parse('${baseUrl}/api/v1/measurements/create'),
body: {"description": description});
var data = response.body;
return MesureModel.fromJson(jsonDecode(response.body));
}
这是我的模型:
List<MesureModel> mesureModelFromJson(String str) => List<MesureModel>.from(
json.decode(str).map((x) => MesureModel.fromJson(x)));
String mesuretModelToJson(List<MesureModel> data) =>
json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
class MesureModel {
MesureModel(
{required this.measurement_id,
required this.measured_value,
required this.taken_by,
required this.param_name,
required this.description});
int measurement_id;
double measured_value;
String taken_by;
String param_name;
String description;
factory MesureModel.fromJson(Map<String, dynamic> json) => MesureModel(
measurement_id: json["measurement_id"],
measured_value: json["measured_value"],
taken_by: json["taken_by"],
param_name: json["param_name"],
description: json["description"]);
Map<String, dynamic> toJson() => {
"measurement_id": measurement_id,
"measured_value": measured_value,
"taken_by": taken_by,
"param_name": param_name,
"description": description
};
}
如果有人能帮助我,我将不胜感激!
Future<MesureModel?> postMeasurement(String description) async {
final response = await http.post(
Uri.parse('${baseUrl}/api/v1/measurements/create'),
headers: {
'Content-Type': 'application/json',
},
body: json.encode({"input_description": description}));
var data = response.body;
return MesureModel.fromJson(jsonDecode(response.body));
}
Json 在发送前对正文进行编码
您应该指定 Content-Type
并对负载进行编码
final response = await http.post(
Uri.parse('${baseUrl}/api/v1/measurements/create'),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(<String, String>{
"description": description,
}),
);
var data = response.body;
return MesureModel.fromJson(jsonDecode(response.body));
我正在尝试使用 post http 方法将数据发送到我的服务器。后端工作完美(用 postman 进行测试)但是我在 flutter 中遇到了这个错误!
这是我的代码:
Future<MesureModel?> postMeasurement(String description) async {
final response = await http.post(
Uri.parse('${baseUrl}/api/v1/measurements/create'),
body: {"description": description});
var data = response.body;
return MesureModel.fromJson(jsonDecode(response.body));
}
这是我的模型:
List<MesureModel> mesureModelFromJson(String str) => List<MesureModel>.from(
json.decode(str).map((x) => MesureModel.fromJson(x)));
String mesuretModelToJson(List<MesureModel> data) =>
json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
class MesureModel {
MesureModel(
{required this.measurement_id,
required this.measured_value,
required this.taken_by,
required this.param_name,
required this.description});
int measurement_id;
double measured_value;
String taken_by;
String param_name;
String description;
factory MesureModel.fromJson(Map<String, dynamic> json) => MesureModel(
measurement_id: json["measurement_id"],
measured_value: json["measured_value"],
taken_by: json["taken_by"],
param_name: json["param_name"],
description: json["description"]);
Map<String, dynamic> toJson() => {
"measurement_id": measurement_id,
"measured_value": measured_value,
"taken_by": taken_by,
"param_name": param_name,
"description": description
};
}
如果有人能帮助我,我将不胜感激!
Future<MesureModel?> postMeasurement(String description) async {
final response = await http.post(
Uri.parse('${baseUrl}/api/v1/measurements/create'),
headers: {
'Content-Type': 'application/json',
},
body: json.encode({"input_description": description}));
var data = response.body;
return MesureModel.fromJson(jsonDecode(response.body));
}
Json 在发送前对正文进行编码
您应该指定 Content-Type
并对负载进行编码
final response = await http.post(
Uri.parse('${baseUrl}/api/v1/measurements/create'),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(<String, String>{
"description": description,
}),
);
var data = response.body;
return MesureModel.fromJson(jsonDecode(response.body));