我无法TextEditingController
在 JSON 正文中发布 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) {},
),
),
Padding(
padding:
const EdgeInsets.only(bottom: 15, left: 10, right: 10),
child: TextFormField(
controller: email,
keyboardType: TextInputType.text,
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) {},
),
),
Padding(
padding:
const EdgeInsets.only(bottom: 15, left: 10, right: 10),
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) {},
),
),
Padding(
padding:
const EdgeInsets.only(bottom: 15, left: 10, right: 10),
child: TextFormField(
controller: password,
keyboardType: TextInputType.text,
decoration: buildInputDecoration(Icons.lock, "Password"),
validator: (String? value) {
if (value!.isEmpty) {
return "Please enter password";
}
return null;
},
),
),
Padding(
padding:
const EdgeInsets.only(bottom: 15, left: 10, right: 10),
child: TextFormField(
controller: confirmpassword,
obscureText: true,
keyboardType: TextInputType.text,
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}");
}
}