When I test the php codes with postman, I get the answer "success". But when we do it with flutter, the code does not enter the if structure or the else structure, the prints do not work. I tried without json_encode but it still didn't work.When I try with Dio, I see the same unresponsiveness again.What can I do?
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'login.dart';
abstract class LoginViewModel extends State<Login> {
final TextEditingController email_controller = TextEditingController();
final TextEditingController pass_controller = TextEditingController();
Future<void> fetchLogin() async {
try {
final response = await http.post(Uri.parse("http://********/canlidestek/login.php"),
body: {"email": email_controller.text, "password": pass_controller.text});
if (response.statusCode == 200) {
var data = json.decode(response.body);
print(data);
}
} catch (e) {
print(e);
}
}
}
PHP
<?php
session_start();
include_once "config.php";
if($_POST){
$email = $_POST['email'];
$password = $_POST['password'];
if(!empty($email) && !empty($password)){
$sql = mysqli_query($conn, "SELECT * FROM livesup WHERE email = '{$email}'");
if(mysqli_num_rows($sql) > 0){
$row = mysqli_fetch_assoc($sql);
$status = "Online";
$sql2 = mysqli_query($conn, "UPDATE livesup SET status = '{$status}' WHERE unique_id = {$row['unique_id']}");
if($sql2){
$_SESSION['unique_id'] = $row['unique_id'];
echo json_encode("success");
}else{
echo "Something went wrong. Please try again!";
}
}else{
echo "$email - This email not Exist!";
}
}else{
echo "All input fields are required!";
}
}else
{
echo "no data post";
}
?>
CodePudding user response:
Description
Since you are not using an online server, and the api working great with post man. we can say that the problem is in connection between your android device and php server (Xampp) which is your laptop.
First Solution to try:
if you using local host on flutter code, change it to laptop IP Address, and the android device and laptop have to be on same network.
Second Solution if the first not working:
you have to make sure to shutdown windows firewall, it will block the request in some cases.
And Make Sure your server accessible from outside devices.