微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法使用 JSON 正文中的 TextEditingController 变量发布 http 请求

如何解决无法使用 JSON 正文中的 TextEditingController 变量发布 http 请求

我无法在 JSON 正文中使用 TextEditingController 发布 http 请求,但如果我直接将字符串传递给 JSON 正文,则相同的代码可以工作

    class _FormPageState extends State<FormPage> {
      late String name1;
      late String email1;
      late String phone1;
      late String pass1;
      late String pass2;
      TextEditingController name = new TextEditingController();
      TextEditingController email = new TextEditingController();
      TextEditingController phone = new TextEditingController();
      TextEditingController password = new TextEditingController();
      TextEditingController confirmpassword = new TextEditingController();
    
      final GlobalKey<FormState> _formkey = GlobalKey<FormState>();
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
            child: SingleChildScrollView(
              child: Form(
                key: _formkey,child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,children: [
                    CircleAvatar(
                      radius: 70,child: Image.network(
                          "https://protocoderspoint.com/wp-content/uploads/2020/10/PROTO-CODERS-POINT-logo-water-mark-.png"),),SizedBox(
                      height: 15,Padding(
                      padding:
                          const EdgeInsets.only(bottom: 15,left: 10,right: 10),child: TextFormField(
                        controller: name,keyboardType: TextInputType.text,decoration: buildInputdecoration(Icons.person,"Full Name"),validator: (String? value) {
                          if (value!.isEmpty) {
                            return "Please enter name";
                          }
                          return null;
                        },onSaved: (String? name) {},child: TextFormField(
                        controller: email,decoration: buildInputdecoration(Icons.email,"Email"),validator: (String? value) {
                          if (value!.isEmpty) {
                            return "Please enter  email";
                          }
                          if (!RegExp("^[a-zA-Z0-9+_.-]+@[a-zA-Z0-9.-]+.[a-z]")
                              .hasMatch(value)) {
                            return "Please enter valid email";
                          }
                          return null;
                        },onSaved: (String? email) {},child: TextFormField(
                        controller: phone,keyboardType: TextInputType.number,decoration: buildInputdecoration(Icons.phone,"Phone No"),validator: (String? value) {
                          if (value!.isEmpty) {
                            return "Please enter  phone";
                          }
                          if (value.length < 9) {
                            return "Please enter valid phone";
                          }
                          return null;
                        },onSaved: (String? phone) {},child: TextFormField(
                        controller: password,decoration: buildInputdecoration(Icons.lock,"Password"),validator: (String? value) {
                          if (value!.isEmpty) {
                            return "Please enter password";
                          }
    
                          return null;
                        },child: TextFormField(
                        controller: confirmpassword,obscureText: true,decoration:
                            buildInputdecoration(Icons.lock,"Confirm Password"),validator: (String? value) {
                          if (value!.isEmpty) {
                            return "Please enter re-password";
                          }
                          if (password.text != confirmpassword.text) {
                            return "Password Do not match";
                          }
                          return null;
                        },SizedBox(
                      width: 200,height: 50,child: RaisedButton(
                        color: Colors.redAccent,onpressed: () {
                          if (_formkey.currentState!.validate()) {
                            RegistrationUser();
                            print("Successful");
                          } else {
                            print("Unsuccessfull");
                          }
                        },shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(50.0),side: BorderSide(color: Colors.blue,width: 2)),textColor: Colors.white,child: Text("Submit"),)
                  ],);
      }
    
      Future RegistrationUser() async {
        var APIURL =
            "https://heythere14168465465.000webhostapp.com/registration.PHP";
        name1 = name.text;
        email1 = email.text;
        phone1 = phone.text;
        pass1 = password.text;
        pass2 = 'd55';
        print(name1);
        print(email1);
        print(phone1);
        print(pass1);
    
        http.Response reponse = await http.post(Uri.parse(APIURL),body: {
        
          "name": name1,"email": email1,"phone": phone1,"password": pass1
        });
        //getting response from PHP code,here
        var data = jsonDecode(reponse.body);
        print("DATA: ${data}");
      }
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。