颤动 ||当用户单击复选框时如何向服务器发送 Put 请求
我想通过互联网执行更新数据,当用户单击复选框时,它应该更改为刻度线完成,并将放置请求发送到服务器,在用户单击复选框之前,manualBillCompletion 变为 true ,它处于 false 状态。
我已经得到了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
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在onChanged内调用ordersPutRequest()函数。
您可以将 orderPutRequest 设为静态以使其可调用。
或使用任何状态管理解决方案。
我希望这能起作用!
You can call the ordersPutRequest() Function inside onChanged.
You can make ordersPutRequest static to make it Callable.
or use any State Management Solution.
I hope this works!