Search code examples
flutterapipopuptextfielddio

flutter: how to pass textfield value in api body


I am a datatable and inserting data in rows from api, and adding edit icon too. so when i click on edit icon a pop up dialogue box appears and it has 3 textfields, date , time in and time out, so when user enter text on time in and time out it should send it api body, but when i do this it gives me this error

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]

here is my code where i am doing this scenario


 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'  );
  }

here is the snap of error

enter image description here

updated code:


  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'  );
}

updated code


 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'  );
}



what am I doing wrong?


Solution

  • First of all, you are sure that you instantiate correctly these controllers like

    timeinController = new TextEditingController();
    

    And looking at the way you are using it, I suppose you're adding listeners to this controllers. Secondly, what types are 'NewTimeIn' and 'NewTimeIn'. You're getting plain strings from your controllers efen if we're talking about time operations here.