颤振:如何在 api 正文中传递文本字段值

时间:2021-06-25 06:06:03

标签: flutter api popup textfield dio

我是一个数据表,从 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'  );
  }

这里是错误的快照

enter image description here

更新代码:


  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 个答案:

答案 0 :(得分:1)

首先,您确定正确实例化这些控制器,例如

timeinController = new TextEditingController();

看看您使用它的方式,我想您正在向该控制器添加侦听器。 其次,什么类型是“NewTimeIn”和“NewTimeIn”。如果我们在这里讨论时间操作,那么您将从控制器 efen 获得纯字符串。