颤振:如何在 api 正文中传递文本字段值
Posted
技术标签:
【中文标题】颤振:如何在 api 正文中传递文本字段值【英文标题】:flutter: how to pass textfield value in api body 【发布时间】:2021-09-08 13:34:59 【问题描述】:我是一个数据表,从 api 中插入数据,并添加编辑图标。所以当我点击编辑图标时,会出现一个弹出对话框,它有 3 个文本字段,日期、时间和超时,所以当用户按时间输入文本和超时时,它应该发送它 api 正文,但是当我这样做时它给了我这个错误
I/flutter (25853): date:TextEditingController#b256a(TextEditingValue(text: ┤├, selection: TextSelection(baseOffset: -1, extentOffset: -1, affinity: TextAffinity.downstream, isDirectional: false), composing: TextRange(start: -1, end: -1))) timein: TextEditingController#dbef7(TextEditingValue(text: ┤├, selection: TextSelection(baseOffset: -1, extentOffset: -1, affinity: TextAffinity.downstream, isDirectional: false), composing: TextRange(start: -1, end: -1))) timeout: TextEditingController#cc4d8(TextEditingValue(text: ┤├, selection: TextSelection(baseOffset: -1, extentOffset: -1, affinity: TextAffinity.downstream, isDirectional: false), composing: TextRange(start: -1, end: -1)))
I/flutter (25853): DioError [DioErrorType.response]: Http status error [404]
这是我正在执行此方案的代码
AlertDialog alert = AlertDialog(
scrollable: true,
//contentPadding:EdgeInsets.fromLTRB(0, 20, 0, 20),
insetPadding: EdgeInsets.symmetric(vertical: 50),
//contentPadding: const EdgeInsets.all(16.0),
title: Text("Request to change time",style: TextStyle(fontWeight: FontWeight.bold,color: Colors.blue[500])),
content:Container(child: SingleChildScrollView(
scrollDirection: Axis.vertical,
child:Column(children:<Widget> [
TextField(
decoration: InputDecoration(labelText: date,hintText: "Date"),
controller:dateController ,
),
TextField(
decoration: InputDecoration(labelText: timein,hintText: "Time in"),
controller:timeinController ,
),
TextField(
decoration: InputDecoration(labelText:timeout,hintText: "Time out"),
controller:timeoutController ,
),
]),
)
),
// Widget okButton = FlatButton(
// child: Text("Submit",style: TextStyle(fontWeight: FontWeight.bold,color: Colors.blue[500],fontSize: 20),),
// onPressed: ()
// markCorrectionApi();
// ,
// );
actions: [
FlatButton(
child: Text("Submit",style: TextStyle(fontWeight: FontWeight.bold,color: Colors.blue[500],fontSize: 20),),
onPressed: ()
Dio dio=new Dio();
var data=
'_id': id,
'TimeIn': timein,
'TimeOut':timeout,
'NewTimeIn':timeinController.text.toString(),
'NewTimeOut':timeoutController.text.toString(),
;
dio
.post(localhostUrlMarkCorrection,data: json.encode(data))
.then((onResponse) async
print("mark correction");
print(onResponse.data);
print(onResponse.statusCode);
).catchError((onerror)
print(onerror.toString());
showAlertDialog(context);
);
)],
);
showDialog(
context: context,
builder: (BuildContext context)
return alert;
,
);
print('date:$dateController timein: $timeinController timeout: $timeoutController' );
print('date:$date timein: $timein timeout: $timeout' );
这里是错误的快照
更新代码:
String timeinText="";
String timeoutText="";
void getupdatedTime()
setState(()
timeinText = timeinController.text;
timeoutText=timeoutController.text;
);
void _getSelectedRowInfo(dynamic date,dynamic timein,dynamic timeout)
AlertDialog alert = AlertDialog(
scrollable: true,
insetPadding: EdgeInsets.symmetric(vertical: 50),
title: Text("Request to change time",style: TextStyle(fontWeight: FontWeight.bold,color: Colors.blue[500])),
content:Container(child: SingleChildScrollView(
scrollDirection: Axis.vertical,
child:Column(children:<Widget> [
TextField(
decoration: InputDecoration(labelText: date,hintText: "Date"),
controller:dateController ,
),
TextField(
decoration: InputDecoration(labelText: timein,hintText: "Time in"),
controller:timeinController ,
),
TextField(
decoration: InputDecoration(labelText:timeout,hintText: "Time out"),
controller:timeoutController ,
),
]),
)
),
// Widget okButton = FlatButton(
// child: Text("Submit",style: TextStyle(fontWeight: FontWeight.bold,color: Colors.blue[500],fontSize: 20),),
// onPressed: ()
// markCorrectionApi();
// ,
// );
actions: [
FlatButton(
child: Text("Submit",style: TextStyle(fontWeight: FontWeight.bold,color: Colors.blue[500],fontSize: 20),),
onPressed: ()
getupdatedTime();
Dio dio=new Dio();
var data=
'_id': id,
'TimeIn': timein,
'TimeOut':timeout,
'NewTimeIn':timeinText,
'NewTimeOut':timeoutText,
;
dio
.post(localhostUrlMarkCorrection,data: json.encode(data))
.then((onResponse) async
print("mark correction");
print(onResponse.data);
print(onResponse.statusCode);
).catchError((onerror)
print(onerror.toString());
// showAlertDialog(context);
);
)],
);
showDialog(
context: context,
builder: (BuildContext context)
return alert;
,
);
print('date:$dateController timein: $timeinController timeout: $timeoutController' );
print('date:$date timein: $timein timeout: $timeout' );
更新代码
void _getSelectedRowInfo(dynamic id,dynamic date,dynamic timein,dynamic timeout)
AlertDialog alert = AlertDialog(
scrollable: true,
insetPadding: EdgeInsets.symmetric(vertical: 50),
title: Text("Request to change time",style: TextStyle(fontWeight: FontWeight.bold,color: Colors.blue[500])),
content:Container(child: SingleChildScrollView(
scrollDirection: Axis.vertical,
child:Column(children:<Widget> [
TextField(
decoration: InputDecoration(labelText: date,hintText: "Date"),
controller:dateController ,
),
TextField(
decoration: InputDecoration(labelText: timein,hintText: "Time in"),
controller:timeinController ,
),
TextField(
decoration: InputDecoration(labelText:timeout,hintText: "Time out"),
controller:timeoutController ,
),
]),
)
),
// Widget okButton = FlatButton(
// child: Text("Submit",style: TextStyle(fontWeight: FontWeight.bold,color: Colors.blue[500],fontSize: 20),),
// onPressed: ()
// markCorrectionApi();
// ,
// );
actions: [
FlatButton(
child: Text("Submit",style: TextStyle(fontWeight: FontWeight.bold,color: Colors.blue[500],fontSize: 20),),
onPressed: ()
getupdatedTime();
Dio dio=new Dio();
var data=
'id': id,
'token':getaccesstoken,
'TimeIn': "4:40:40 PM",
'TimeOut':"4:50:50 PM",
;
print("token is "+getaccesstoken);
print("submit id is "+id);
print(data);
dio
.post(localhostUrlMarkCorrection, data: json.encode(data)) //when i do debugging it not running after this line
.then((onResponse) async
print("mark correction");
print(onResponse.data);
print(onResponse.statusCode);
).catchError((onerror) //it comes here and print 404 error
print(onerror.toString());
// showAlertDialog(context);
);
)],
);
showDialog(
context: context,
builder: (BuildContext context)
return alert;
,
);
// print('date:$dateController timein: $timeinController timeout: $timeoutController' );
// print('date:$date timein: $timein timeout: $timeout' );
我做错了什么?
【问题讨论】:
【参考方案1】:首先,你确定你正确地实例化了这些控制器,比如
timeinController = new TextEditingController();
看看你使用它的方式,我想你正在向这个控制器添加监听器。 其次,“NewTimeIn”和“NewTimeIn”是什么类型。如果我们在这里讨论时间操作,您会从控制器中获取纯字符串。
【讨论】:
我将 timeinController 值传递给 NewTimeIn 和 NewTimeOut 相同,我输入 4:50:50 之类的输入 你在为post操作设置标题吗? 我更新了我的代码,我在调试时得到了 timeinText 和 timeoutText 中的值,但仍然得到同样的错误 我在正文中传递有效载荷/ api 参数,而不是使用标头 您可以使用诸如 timeinController.addListener(() timeinText = timeInController.text; ); 之类的侦听器来摆脱 getupdatedTime() 函数。我还是觉得api调用有问题。以上是关于颤振:如何在 api 正文中传递文本字段值的主要内容,如果未能解决你的问题,请参考以下文章