flutter DioError [bad response]:请求返回无效的状态代码500

hfsqlsce  于 2023-05-23  发布在  Flutter
关注(0)|答案(1)|浏览(1172)

我想发布包含主题数组的数据与dio包在Flutter,但我得到了这个错误:
“DioError [错误响应]:请求返回无效的状态代码500。”
dio包版本是5.1.2,我也试过旧版本。
这是我的post方法请求代码:

Dio()
        .post('${Urls.baseUrl}/vendor/v1/stores/updateItemByGroup',
        options: Options(
          headers: {
            "Accept": "application/json",
            "Authorization": "Bearer $token"
          },
        ),
        queryParameters: queryParameters)

这是我在下面传递给post方法的查询参数:

var data = {
   "store_id": widget.storeId.toString(),
   "for": "stock",
   "same_amount": 'false'
    };
   for (int i = 0;i < widget.items.data!.length;i++) {
    data.addAll({
     "items[$i]": jsonEncode(ref
     .watch(editStocksProvider.notifier)
      .state[i])
    });
}

我也试着用这个模型发送它,但它不工作:

class EditedStockDataModel {
   String? storeId;
   List<EditedStockModel>? items;
   String? type;
   String? sameAmount;

  EditedStockDataModel( {this.type, this.sameAmount,this.storeId, this.items});

   EditedStockDataModel.fromJson(Map<String, dynamic> json) {
     storeId = json['store_id'];
     if (json['items'] != null) {
       items = <EditedStockModel>[];
       json['services'].forEach((v) {
         items!.add( EditedStockModel.fromJson(v));
       });
     }
     type = json['type'];
     sameAmount = json['same_amount'];
   }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['store_id'] = this.storeId;
    data['items'] = this.items;
    data['for'] = this.type;
    data['same_amount'] = this.sameAmount;
    if (this.items != null) {
      data['services'] = this.items!.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class EditedStockModel {
   int? id;
   String? stock;
   String? maxPerOrder;

  EditedStockModel({this.id, this.stock, this.maxPerOrder});

   EditedStockModel.fromJson(Map<String, dynamic> json) {
     id = json['id'];
     stock = json['stock'];
     maxPerOrder = json['max_per_order'];
   }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['stock'] = this.stock;
    data['max_per_order'] = this.maxPerOrder;
    return data;
  }
}

这就是请求在Postman中的工作原理:

hivapdat

hivapdat1#

看起来服务器无法查询您发送给他们的参数。
对于第一种方法

for (int i = 0;i < widget.items.data!.length;i++) {
    data.addAll({
     "items[$i]": jsonEncode(ref
     .watch(editStocksProvider.notifier)
      .state[i])
    });

此参数应采用以下格式:

{
    "item1": {
        "stock": 1,
        "id": 1,
        "max_per_order": "2"
    },
    "item2":  {
        "stock": 1,
        "id": 1,
        "max_per_order": "2"
    } 
   
}

这不像你的 Postman 参数
EditedStockDataModel模型中的toJson函数,items属性应该通过调用

Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['stock'] = this.stock;
    data['max_per_order'] = this.maxPerOrder;
    return data;
  }

请尝试将代码修改为

Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['store_id'] = this.storeId;
    data['items'] = items?.map((e) => e.toJson()).toList();
    data['for'] = this.type;
    data['same_amount'] = this.sameAmount;
    if (this.items != null) {
      data['services'] = this.items!.map((v) => v.toJson()).toList();
    }
    return data;
  }

相关问题