颤动 ||当用户单击复选框时如何向服务器发送 Put 请求

发布于 2025-01-09 00:58:17 字数 4570 浏览 2 评论 0原文

我想通过互联网执行更新数据,当用户单击复选框时,它应该更改为刻度线完成,并将放置请求发送到服务器,在用户单击复选框之前,manualBillCompletion 变为 true ,它处于 false 状态。

这是我对 API 的响应 输入图片这里的描述

我已经得到了flutter docs的帮助并创造了services.dart 和模型数据。 但当用户单击复选框时,我无法在 UI 上表示。

这是我的 services.dart

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

    http.Response response;
   response = await http.put(Uri.parse(ordersPut),headers: <String,String>{
      'Authorization' : 'Bearer $jwt',
        'Content-Type' : 'multipart/form-data; boundary=---01xxxxxx'
     },
         body: jsonEncode(<String,String>{
    'manualBillCompletion': data.manualBillCompletion!,
    'mobile': data.balancePayment!
      })
 );

  if(response.statusCode == 200) {
print(response.statusCode);
String responseBody = response.body;

return OrdersPut.fromJson(jsonDecode(responseBody));
 }
 else {
     print("error ${response.statusCode}");
      throw Exception('Failed to load data');
 }
}

这是我的类

  class ApiOrdersData{
   String? manualBillCompletion;
    String? balancePayment;

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

,这是我的变量,我在其中存储获取响应

bool? checkedValue = order?.data.attributes.totalBills[index].manualBillCompletion ?? false;

,这是我的 UI,我在其中使用获取响应来表示在屏幕上。

                  checkedValue == 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: checkedValue,
                                                onChanged: (newValue) {
                                                  setState(() {
                                                    checkedValue = newValue;
                                                  });
                                                }),
                                          ],
                                        )
                                     :
                            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: checkedValue,
                                                onChanged: (newValue) {
                                                  setState(() {
                                                    checkedValue = newValue;
                                                  });
                                                }),
                                          ],
                                        ),

I want to perform Update data over the internet when the user clicks on the checkbox it should change to tick mark done and send put request to the server where manualBillCompletion turns: true before the user clicks on the checkbox it was on false.

here is my response of the API
enter image description here

I have taken the help of flutter docs and created the services.dart and model data.
But I am not able to represent on UI when the user clicks on the checkbox.

here are my services.dart

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

    http.Response response;
   response = await http.put(Uri.parse(ordersPut),headers: <String,String>{
      'Authorization' : 'Bearer $jwt',
        'Content-Type' : 'multipart/form-data; boundary=---01xxxxxx'
     },
         body: jsonEncode(<String,String>{
    'manualBillCompletion': data.manualBillCompletion!,
    'mobile': data.balancePayment!
      })
 );

  if(response.statusCode == 200) {
print(response.statusCode);
String responseBody = response.body;

return OrdersPut.fromJson(jsonDecode(responseBody));
 }
 else {
     print("error ${response.statusCode}");
      throw Exception('Failed to load data');
 }
}

here is my class

  class ApiOrdersData{
   String? manualBillCompletion;
    String? balancePayment;

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

here is my variable where I am storing the get response

bool? checkedValue = order?.data.attributes.totalBills[index].manualBillCompletion ?? false;

here is my UI where I am using get a response to represent on screen.

                  checkedValue == 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: checkedValue,
                                                onChanged: (newValue) {
                                                  setState(() {
                                                    checkedValue = newValue;
                                                  });
                                                }),
                                          ],
                                        )
                                     :
                            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: checkedValue,
                                                onChanged: (newValue) {
                                                  setState(() {
                                                    checkedValue = newValue;
                                                  });
                                                }),
                                          ],
                                        ),

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情独悲 2025-01-16 00:58:17

您可以在onChanged内调用ordersPutRequest()函数。

     Checkbox(
     focusColor: Color(0xff1170DE),
     activeColor: Color(0xff1170DE),
     shape: CircleBorder(),
     value: checkedValue,
     onChanged: (newValue) {
     setState(() {
         checkedValue = newValue;
    });
    ordersPutRequest(ApiOrdersData(manualBillCompletion:"true",balancePayment:balancePayment
    ))
 }),

您可以将 orderPutRequest 设为静态以使其可调用。

static Future <OrdersPut> ordersPutRequest(ApiOrdersData data)

或使用任何状态管理解决方案。

我希望这能起作用!

You can call the ordersPutRequest() Function inside onChanged.

     Checkbox(
     focusColor: Color(0xff1170DE),
     activeColor: Color(0xff1170DE),
     shape: CircleBorder(),
     value: checkedValue,
     onChanged: (newValue) {
     setState(() {
         checkedValue = newValue;
    });
    ordersPutRequest(ApiOrdersData(manualBillCompletion:"true",balancePayment:balancePayment
    ))
 }),

You can make ordersPutRequest static to make it Callable.

static Future <OrdersPut> ordersPutRequest(ApiOrdersData data)

or use any State Management Solution.

I hope this works!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文