formatexception unexpected end of input (at character 1)

70 Views Asked by At

I'm currently working on an app with a friend, and we're using a local SQL database, one on each PC, with identical names, tables, and fields. Essentially, it's the same database. The issue arises when my friend, from his PC, tries to execute this query:

void getSuggestion() async {
  var res = await http.post(
    Uri.parse(API.search),
    body: {
      "query": query,
    },
  );

  if (res.statusCode == 200) {
    setState(() {
      var data = json.decode(res.body);
    });
  } else {
    setState(() {
      error = true;
    });
  }
}

He encounters the error: "FormatException: Unexpected end of input (at character 1)" on this line:

var data = json.decode(res.body);

Printing the response with printf shows an empty response. What could be causing it to work on one PC but not on another? We are both using physical devices for testing, and each PHP has the IP corresponding to our respective networks, with the notebook on WiFi and my PC on Ethernet

Why is this happening? Thank you very much.

I tried to print the res.body from my friend and got an empty string, This is the php:

<?php

include '../connection.php';

if(isset($_REQUEST["query"])){
  $query = strtolower($_REQUEST["query"]);
}else{
  $query = "";
} //Si hay una query (sea GET, POST; ETC) asignarlo

$json["error"] = false;
$json["errmsg"] = "";
$json["data"] = array();
$sql = "SELECT * FROM clipro ORDER BY nombre ASC";
$res = $connectNow->query($sql);
$numrows = mysqli_num_rows($res);

if($numrows > 0){
  //Si hay datos

  $namelist = array();

  while($obj = mysqli_fetch_object($res)){
    $matching = str_contains(strtolower($obj->nombre), strtolower($query)) ? 100 : 0;

    //Similaridad entre nombres

    $namelist[$matching][$obj->nro_cuenta] = array(
      'id' => $obj->nro_cuenta,
      'name' => $obj->nombre,
      'direccion' => $obj->direccion,
      'tel' => $obj->telefono
    );
  }

  krsort($namelist);

  //to sort array by key in desending order, use ksort() to reverse

  foreach($namelist as $innerarray){
    foreach ($innerarray as $nro_cuenta => $data) {
      $subdata = array(); // create a new array
      $subdata["id"] = "$nro_cuenta";
      $subdata["name"] = $data["name"];
      $subdata["direccion"] = $data["direccion"];
      $subdata["tel"] = $data["tel"];
      array_push($json["data"], $subdata);
    }
  }
}
else{
  $json["error"] = true;
  $json["errmsg"] = "No hay datos que mostrar.";
}

mysqli_close($connectNow);
header('Content-Type: application/json');

// tell browser that its a json data
echo json_encode($json);
?>
0

There are 0 best solutions below