Php 异常已发生

Php 异常已发生,php,flutter,Php,Flutter,我有一个从数据库读取数据的方法。代码完全正常工作,但有一个问题,如果我运行代码,但数据库中没有数据,我会得到错误: E/flutter (29414): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: FormatException: Unexpected character (at character 1) E/flutter (29414): No Data Found. E/flutter (29414):

我有一个从数据库读取数据的方法。代码完全正常工作,但有一个问题,如果我运行代码,但数据库中没有数据,我会得到错误:

E/flutter (29414): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: FormatException: Unexpected character (at character 1)
E/flutter (29414): No Data Found.
E/flutter (29414): ^


我的代码是:

Future<List<data>> FetchT() async {
    apiURL = 'https://*******.com/getList.php?C=' +
        gatName.toString();
    var response = await http.get(apiURL);
    if (response.statusCode == 200) {
      final items = json.decode(response.body).cast<Map<String, dynamic>>();
      List<data> listOfFruits = items.map<data>((json) {
        return data.fromJson(json);
      }).toList();

      return listOfFruits;
    } else {
      //throw Exception('Failed to load data from Server.');
    }
  }

Future FetchT()异步{
apiURL='https://********.com/getList.php?C='1!'+
gatName.toString();
var response=wait http.get(apirl);
如果(response.statusCode==200){
final items=json.decode(response.body.cast();
List listofruits=items.map((json){
返回data.fromJson(json);
}).toList();
归还水果清单;
}否则{
//抛出异常('未能从服务器加载数据');
}
}
PHP代码:


<?php

include 'connt.php';
        $C= $_GET["C"];
$sql = "SELECT * FROM top WHERE C=? ORDER BY id DESC ";

$result = $con->query($sql);


$stmt = $con->prepare($sql); 

$stmt->bind_param("s",$C);

$stmt->execute();

$result = $stmt->get_result();



 
if ($result->num_rows >0) {
 
 
     while($row[] = $result->fetch_assoc()) {
     
     $item = $row;
     
     $json = json_encode($item, JSON_NUMERIC_CHECK);
     
     }
 
} else {
    echo "No Data Found.";
}
 echo $json;
$con->close();

?>

我以前在这里搜索了老主题,并尝试了所有与我类似的主题和问题,但我没有找到解决问题的方法


如何解决此问题?

如果找不到数据,您没有指定标题代码

您应该将php代码更改为:

<?php

include 'connt.php';
$C= $_GET["C"];
$sql = "SELECT * FROM top WHERE C=? ORDER BY id DESC ";

$result = $con->query($sql);


$stmt = $con->prepare($sql); 

$stmt->bind_param("s",$C);

$stmt->execute();

$result = $stmt->get_result();



 
if ($result->num_rows >0) {
 
 
     while($row[] = $result->fetch_assoc()) {
     
     $item = $row;
     
     $json = json_encode($item, JSON_NUMERIC_CHECK);
     
     }
 
} else {
    http_response_code(404);
    $json = json_encode(["error" => "No Data Found."]);
}
 echo $json;
$con->close();

?>