gpt4 book ai didi

api - 尝试发布到 API 时,我得到 "status": 400 error in Flutter

转载 作者:行者123 更新时间:2023-12-05 04:49:31 27 4
gpt4 key购买 nike

I/flutter (30384): data {"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1","title":"出现一个或多个验证错误。 ","status":400,"traceId":"|e3244589-485510f2c271c42c.","errors":{"$.name":["'name, "surname": 姓氏, "email": 电子邮件, "plateNumber “:plateNumber}' 是无效的 JSON 文字。应为文字 'null'。路径:$.name | LineNumber:0 | BytePositionInLine:11。”]}}

代码在这里:

数据模型类

List<DataModel> dataModelFromJson(String str) =>
List<DataModel>.from(json.decode(str).map((x) => DataModel.fromJson(x)));

String dataModelToJson(List<DataModel> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson())));

class DataModel {
DataModel({
this.name,
this.surname,
this.email,
this.plateNumber,
});

int id;
String name;
String surname;
String email;
String plateNumber;

factory DataModel.fromJson(Map<String, dynamic> json) => DataModel(
name: json["name"],
surname: json["surname"],
email: json["email"],
plateNumber: json["plateNumber"],
);

Map<String, dynamic> toJson() => {
"name": name,
"surname": surname,
"email": email,
"plateNumber": plateNumber,
};
}

服务等级

Future<DataModel> submitData(String name, String surname, String email, String plateNumber) async {
try {
Uri uri = Uri.parse("http://ngcyaz.com/api/drivers");
String json = "{ \"name\": name, \"surname\": surname, \"email\": email, \"plateNumber\": plateNumber}";
Map<String, String> header = {"content-type": "application/json"};
var response = await http.post(uri, body: json, headers: header);
var data = response.body;
print("data " + data);
if (response.statusCode == 200) {
String responseString = response.body;
dataModelFromJson(responseString);
} else
return null;
} catch (e) {
print(e);
}
}

最佳答案

使用简单的map并将其传递给jsonEncode

submitData(
String name, String surname, String email, String plateNumber) async {
try {
Uri uri = Uri.parse("http://ngcyaz.com/api/drivers");
final map = {
"name": name,
"surname": surname,
"email": email,
"plateNumber": plateNumber
};
Map<String, String> header = {"content-type": "application/json"};
var response = await http.post(uri, body: jsonEncode(map), headers: header);
var data = response.body;
print("data " + data);
if (response.statusCode == 200) {
String responseString = response.body;
print("Response String: " + responseString);
} else
return null;
} catch (e) {
print(e);
}
}

关于api - 尝试发布到 API 时,我得到 "status": 400 error in Flutter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67556181/

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