I make an register form for insert and user in mysql. I'm using a "Future async" function with FlutterToast for show text if the registration is successfull or not and this is connected with my register.php archive. I need show the text or show an alert message, and that only works when I run in navigators like Chrome, not in Android Studio, when I run appears an error in the terminal only when I run in Android Studio.
D/InputConnectionAdaptor( 6148): The input method toggled cursor monitoring on E/flutter ( 6148): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)]
Unhandled Exception: Invalid argument (onError): Error handler must accept one Object or one Object and a StackTrace as arguments.: Closure: (HttpException) => Null
My function:
Future register() async {
var url =
"http://192.167.1.142/databasedb/register.php";
var response = await http.post(url, body: {
"correo": correo.text,
"celular": celular.text,
"passwd": passwd.text,
"passwd2": passwd2.text
});
var data = json.decode(response.body);
if (data == "Error") {
FlutterToast(context).showToast(
child: Text(
'User invalid',
style: TextStyle(fontSize: 25, color: Colors.red),
));
} else {
FlutterToast(context).showToast(
child: Text('Registration Successful',
style: TextStyle(fontSize: 25, color: Colors.green)));
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DashBoard(),
),
);
}
}
My Elevated Button:
Expanded(
child: ElevatedButton(
style: ElevatedButton.styleFrom(
onSurface: Colors.blue,
),
child: Text('Register',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.white)),
onPressed: () => [
// enabled: isButtonActive,
if (isButtonActive)
{
setState(() => isButtonActive = false),
}
else
{null},
register(), //REGISTER FUNCTION
setState(() {})
],
),
),
My register.php:
<?php
header('Access-Control-Allow-Origin: *');
$db = mysqli_connect('localhost','root','','databasedb');
//Conectar al hosting
if (!$db) {
echo "Database connection faild";
}
$cell= $_POST['celular'];
$email= $_POST['correo'];
$pass= $_POST['passwd'];
$pass2= $_POST['passwd2'];
$sql = "SELECT celular FROM usuario WHERE celular = '".$cell."' AND passwd = '".$pass."'";
$result = mysqli_query($db,$sql);
$count = mysqli_num_rows($result);
if ($count == 1) {
echo json_encode("Error");
}else{
if($pass == $pass2 && strlen($pass) >= 8 && strlen($email) >= 10 && (strlen($cell) >= 9 && strlen($cell) <= 15)){
$insert = "INSERT INTO usuario(correo,celular,passwd,passwd2)VALUES('".$email."','".$cell."','".$pass."','".$pass2."')";
$query = mysqli_query($db,$insert);
if ($query) {
echo json_encode("Success");
}
}else{
echo json_encode("Contraseña inválida");
}
}
?>