尝试发布到 API 时,我在 Flutter 中收到“状态”:400 错误

Posted

技术标签:

【中文标题】尝试发布到 API 时,我在 Flutter 中收到“状态”:400 错误【英文标题】:When trying to post to the API, I get a "status": 400 error in Flutter 【发布时间】:2021-08-05 22:02:11 【问题描述】:

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": surname, "email": 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);
  

【问题讨论】:

在访问 API 之前,请检查给键 name 的值是否为空。 【参考方案1】:

使用简单的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 时,我在 Flutter 中收到“状态”:400 错误的主要内容,如果未能解决你的问题,请参考以下文章

为啥在尝试将firebase添加到flutter时出现此错误“已弃用的API”

将 Google 地图添加到 Flutter 时找不到 API 密钥

使用 API 注册 Flutter

将 JSON 数据显示到 DataTable Flutter

Flutter 如何将地图从互联网 API 转换为列表?

Flutter 队列 API 请求稍后执行