I tried to post some data through API but every time the response is failed i don't knoow why the response is failed because if i normally hit the API in it show me the success response but i hit the API on press button it shows me response Failed.
Here is my code :-
Register.dart :
void main() => runApp(const Register());
class Register extends StatelessWidget {
const Register({super.key});
@override
Widget build(BuildContext context) {
final Size size = MediaQuery.of(context).size;
final ThemeData themeData = Theme.of(context);
const double padding = 25;
const sidePadding = EdgeInsets.symmetric(horizontal: padding);
return Scaffold(
body: SingleChildScrollView(
child: Container(
padding: sidePadding,
width: size.width,
height: size.height,
child: Center(
child: RegisterForm(),
),
),
),
);
}
}
class RegisterForm extends StatefulWidget {
const RegisterForm({super.key});
@override
_RegisterForm createState() => _RegisterForm();
}
class _RegisterForm extends State<RegisterForm> {
TextEditingController _name = new TextEditingController();
TextEditingController _mobileno = new TextEditingController();
final _formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return Form(
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextFormField(
controller: _name,
keyboardType: TextInputType.text,
decoration: InputDecoration(
labelText: 'Technician Name',
labelStyle: const TextStyle(
color: Color(0xfff9a28f),
),
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter some text';
}
return null;
},
),
TextFormField(
controller: _mobileno,
keyboardType: TextInputType.phone,
decoration: InputDecoration(
labelText: 'Enter Mobile Number',
labelStyle: const TextStyle(
color: Color(0xfff9a28f),
),
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter more some text';
}
return null;
},
),
Padding(
padding: EdgeInsets.fromLTRB(20, 0, 20, 0),
child: ElevatedButton(
onPressed: () {
if (_formKey.currentState!.validate()) {
RegisterController(
name: _name.text,
mobileno: _mobileno.text,
).addRegisterAPI().then((value) {
print('Response: $value');
});
}
},
child: Text(
'Register',
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontSize: 20,
),
),
),
),
],
),
);
}
}
registercontroller.dart
import 'package:http/http.dart' as http;
import 'dart:convert';
class RegisterController {
final String name;
final String mobileno;
RegisterController({required this.name, required this.mobileno});
Future<String> addRegisterAPI() async{
try{
final response = await http.post(
Uri.parse("https://********.in/index.php?"), body :{
'act': "register",
'name': name,
'mobile': mobileno,
});
if(response.statusCode == 200){
final result = jsonDecode(response.body);
return result;
} else {
return "fail";
}
} catch (e) {
return "failed";
}
}
}
when onPressed
hit RegisterController()
it print the Response: failed
i don't know why the RegisterController()
goes in catch (e)
and print Response: failed
fir any one knows how to solve this please help me
CodePudding user response:
did you miss the jsonEncode in the body?
final response = await http.post(
Uri.parse("https://********.in/index.php?"), body :jsonEncode({
'act': "register",
'name': name,
'mobile': mobileno,
})
);
CodePudding user response:
final response = await http.post(
Uri.parse("https://********.in/index.php?"),
headers: <String, String>{
'Content-Type': 'application/json',
},
body :jsonEncode(<String, dynamic>{
'act': "register",
'name': name,
'mobile': mobileno,
}));
if it did not help, print the error for more information.