FLutter || getting 500 internal error in put request but works perfectly on postman

Viewed 410

I am working on a checkbox where when the user clicks on the checkbox it will send a put request to the server and the checkbox permanently changes to bluetick. It is working on the postman, not in my flutter app.

here is my put service where I have removed the API link with xxxx:-

  Future <OrdersPutModel> ordersPutRequest(ApiOrdersData data)  async{
  SharedPreferences prefs = await SharedPreferences.getInstance();
  String? jwt = prefs.getString('jwt');

  http.Response response;
  response = await http.put(Uri.parse("http://xxxx/api/orders/19"),headers: 
  <String,String>{
  'Authorization' : 'Bearer $jwt',
   'Content-Type' : 'multipart/form-data; boundary=---011000010111000001101001'
  },
    body: jsonEncode(<String,String>{
         'manualBillCompletion': data.manualBillCompletion!,
         'balancePayment': data.balancePayment!
     })
    );

  if(response.statusCode == 200) {
   print("response.statusCode ${'successful'}");
   return OrdersPutModel.fromJson(jsonDecode(response.body));
  }
 else {
   print("error ${response.statusCode}");
   throw Exception('Failed to load data');
  }
}

my put order class:-

class ApiOrdersData{
 String? manualBillCompletion;
 String? balancePayment;

 ApiOrdersData({
 required this.manualBillCompletion,
 required this.balancePayment
 });
 }

my orders UI code : -

snapshot.data?.data.attributes
                                          .totalBills[index].manualBillCompletion == false
                                          ? Row(
                                        children: [
                                          Text(
                                            "Not Completed",
                                            style: TextStyle(
                                                color: Color.fromRGBO(
                                                    17, 112, 222, 1),
                                                fontSize: 12,
                                                fontWeight: FontWeight.w600),
                                          ),
                                          Checkbox(
                                              focusColor: Color(0xff1170DE),
                                              activeColor: Color(0xff1170DE),
                                              shape: CircleBorder(),
                                              value: snapshot.data?.data.attributes
                                                  .totalBills[index].manualBillCompletion,
                                              onChanged: (newValue) {
                                                setState(() {
                                                  snapshot.data?.data.attributes
                                                      .totalBills[index].manualBillCompletion = newValue!;
                                                });
                                                sendCheckBoxRequest();
                                              }),
                                        ],
                                      )
                                          :
                                      Row(
                                        children: [
                                          Text(
                                            "Completed",
                                            style: TextStyle(
                                                color: Color.fromRGBO(
                                                    17, 112, 222, 1),
                                                fontSize: 12,
                                                fontWeight: FontWeight.w600),
                                          ),
                                          Checkbox(
                                              focusColor: Color(0xff1170DE),
                                              activeColor: Color(0xff1170DE),
                                              shape: CircleBorder(),
                                              value: snapshot.data?.data.attributes
                                                  .totalBills[index].manualBillCompletion,
                                              onChanged: (newValue) {

                                                setState(() {
                                                  // snapshot.data?.data.attributes
                                                  //     .totalBills[index].manualBillCompletion = newValue!;
                                                });
                                                // _futureOrdersModel = ordersPutRequest(ApiOrdersData(manualBillCompletion:'true', balancePayment:'0'));
                                              }),
                                        ],
                                      ),

my error

1 Answers

In body simply pass map variable...donot use json.encode. And try to print statuscode in console.

Related