I am new to flutter. I connected to a local API.I am getting Connection Terminated Error
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:async';
import 'dart:convert';
class Registration extends StatefulWidget {
const Registration({Key? key}) : super(key: key);
@override
State<Registration> createState() => _RegistrationState();
}
class _RegistrationState extends State<Registration> {
TextEditingController _first_controller = TextEditingController();
TextEditingController _last_controller = TextEditingController();
TextEditingController _address_controller = TextEditingController();
TextEditingController _email_controller = TextEditingController();
TextEditingController _phone_controller = TextEditingController();
TextEditingController _gender_controller = TextEditingController();
TextEditingController _password_controller = TextEditingController();
final GlobalKey<FormState> _formkey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.white, resizeToAvoidBottomInset: false,
appBar: AppBar(
centerTitle: true,
title: Text('Registration',
style: TextStyle(
fontSize: MediaQuery.of(context).size.width / 12,
fontWeight: FontWeight.normal,
fontStyle: FontStyle.italic,
color: Colors.white)),
),
body: SafeArea(
child: SingleChildScrollView(
child: Form(
key: _formkey,
child: Container(
height: MediaQuery.of(context).size.height / 1,
child: Column(
children: [
Align(
alignment: Alignment.center,
child: Padding(
padding:
EdgeInsets.all(MediaQuery.of(context).size.width / 12),
child: Column(
children: [
TextFormField(
textInputAction: TextInputAction.next,
controller: _first_controller,
keyboardType: TextInputType.name,
decoration: InputDecoration( labelText: 'First Name', labelStyle: TextStyle( fontSize: MediaQuery.of(context).size.width / 22), border: const OutlineInputBorder(), suffixIcon: const Icon(Icons.person_add_alt_1_outlined), ), validator: (value) { if (value == null || value == "") { return "Please Enter First Name "; } else { return null; } }, ), SizedBox( height: MediaQuery.of(context).size.height / 50, ), TextFormField( textInputAction: TextInputAction.next, controller: _last_controller, keyboardType: TextInputType.name, decoration: InputDecoration( labelText: 'Last Name', labelStyle: TextStyle( fontSize: MediaQuery.of(context).size.width / 22), border: const OutlineInputBorder(), suffixIcon: const Icon(Icons.person_add_alt_1_outlined), ), validator: (value) { if (value == null || value == "") { return "Please Enter Last Name "; } else { return null; } }, ), SizedBox( height: MediaQuery.of(context).size.height / 50, ), TextFormField( textInputAction: TextInputAction.next, controller: _address_controller, keyboardType: TextInputType.streetAddress, decoration: InputDecoration( labelText: 'Address', labelStyle: TextStyle( fontSize: MediaQuery.of(context).size.width / 22), border: const OutlineInputBorder(), suffixIcon: const Icon(Icons.location_city_outlined), ), validator: (value) { if (value == null || value == "") { return "Please Enter Address "; } else { return null; } }, ), SizedBox( height: MediaQuery.of(context).size.height / 50, ), TextFormField( textInputAction: TextInputAction.next, controller: _phone_controller, keyboardType: TextInputType.number, decoration: InputDecoration( labelText: 'Phone Number', labelStyle: TextStyle( fontSize: MediaQuery.of(context).size.width / 22), border: const OutlineInputBorder(), suffixIcon: const Icon(Icons.mobile_friendly_outlined), ), validator: (value) { if (value == null || value == "" || value.length < 8) { return "Please Enter Valid Mobile Number "; } else { return null; } }, ), SizedBox( height: MediaQuery.of(context).size.height / 50, ), TextFormField( textInputAction: TextInputAction.next, controller: _email_controller, keyboardType: TextInputType.emailAddress, decoration: InputDecoration( labelText: 'E-mail', labelStyle: TextStyle( fontSize: MediaQuery.of(context).size.width / 22), border: const OutlineInputBorder(), suffixIcon: const Icon(Icons.email_outlined), ), validator: (value) { if (value == null || value == "") { return "Please Enter E-mail "; } else { return null; } }, ), SizedBox( height: MediaQuery.of(context).size.height / 50, ),
TextFormField(
textInputAction: TextInputAction.next,
controller: _gender_controller,
keyboardType: TextInputType.text,
decoration: InputDecoration(
labelText: 'Gender',
helperText: 'Choice M or F',
labelStyle: TextStyle(
fontSize:
MediaQuery.of(context).size.width / 22),
border: const OutlineInputBorder(),
suffixIcon: const Icon(Icons.male_outlined),
),
validator: (value) {
if (value == null || value == "") {
return "Please Enter Gender";
} else {
return null;
}
},
),
SizedBox(
height: MediaQuery.of(context).size.height / 50,
),
TextFormField(
textInputAction: TextInputAction.done,
controller: _password_controller,
keyboardType: TextInputType.visiblePassword,
decoration: InputDecoration(
labelText: 'Password',
helperText: 'Minimum strength = 3',
labelStyle: TextStyle(
fontSize:
MediaQuery.of(context).size.width / 22),
border: const OutlineInputBorder(),
suffixIcon: const Icon(Icons.visibility),
),
validator: (value) {
if (value == null ||
value == "" ||
value.length < 3) {
return "Please Enter Valid Password";
} else {
return null;
}
},
),
SizedBox(
height: MediaQuery.of(context).size.height / 50,
),
ElevatedButton(
onPressed: () {
RegisterData();
},
child: Text(
'Register',
style: TextStyle(
fontSize:
MediaQuery.of(context).size.width / 22,
fontStyle: FontStyle.italic,
letterSpacing:
MediaQuery.of(context).size.width / 209),
),
)
],
),
),
)
],
),
),
),
)),
);
}
Future RegisterData() async {
var url = "https://192.168.2.109:8085/Users";
Map data = {
'firstName': _first_controller.text,
'lastName': _last_controller.text,
'address': _address_controller.text,
'phonenumber': _phone_controller.text,
'email': _email_controller.text,
'gender': _gender_controller.text,
'password': _password_controller.text,
};
print("JSON DATA: $data");
http.Response response = await http.post(Uri.parse(url), body: data);
var mssg_data = jsonDecode(response.body);
print("DATA: $mssg_data");
}
}
CodePudding user response:
I am getting handshake exceptions whenever I switch the date manually on my device or emulator and forget to put it back. I don't know if that's the problem in your case but give it a try and check if your dates are correct. Also sometimes in emulator it needs to delete it and recreate it to run properly.
CodePudding user response:
You get a handshake exception whenever your app's http client reject a call before it goes to the respective Rest Service. By default, the flutter's http client rejects calls that trying to communicate with an service having expired or unverified security certificate.
You can overwrite your default http client and make it to send requests to a rest service with invalid certificate by using this code example:
void main() {
HttpOverrides.global = MyHttpOverrides();
runApp(MyApp());
}
class MyHttpOverrides extends HttpOverrides {
@override
HttpClient createHttpClient(SecurityContext context) {
return super.createHttpClient(context)
..badCertificateCallback =
(X509Certificate cert, String host, int port) => true;
}
}
Also, if the problem remains, we should investigate it a little bit more. If you use an emulator, maybe, the app cannot communication with your local server, if so, you can solve it by following this thread: How do you connect localhost in the Android emulator?