2

I want to post data that contains array of subjects with dio package in flutter, but I got this error:

"DioError [bad response]: The request returned an invalid status code of 500."

dio package version is 5.1.2, and I tried older versions too.

This is my post method request code:

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

This is query parameters that I pass to post method at below:

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])
    });
}

I tried to send it with this model too, but it doesn't work:

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;
  }
}

This is how request works in postman:

enter image description here

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
dinomim
  • 21
  • 5

1 Answers1

0

Look like the server can't query your parameter sent to them.

For the first approach

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

This parameter should be in this format:

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

It's not look like your postman parameters

The toJson function from EditedStockDataModel model, the items attribute should be convert to json by calling

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;
  }

Please try to modify the code to this

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;
  }