在颤抖中,我使用了一个php文件,该文件从数据库查询返回了json响应,但是当我尝试解码json时,出现此错误:
E/flutter ( 8294): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: FormatException: Unexpected character (at character 1) E/flutter ( 8294): [{"0":"PRUEBA","usu_nombre":"PRUEBA"}] E/flutter ( 8294): ^
这是我的飞镖功能:
Future<String> iniciarSesion() async{ var usuario = textUsuario.text; var password = textPassword.text; var nombreUsuario; var url ="http://192.168.1.37/usuario.php"; //Metodo post var response = await http.post( url, headers:{ "Accept": "application/json" } , body: { "usuario": '$usuario',"password": '$password'}, encoding: Encoding.getByName("utf-8") ); List data = json.decode(response.body); }
以及来自我的php文件的代码:
<?php error_reporting(E_ALL); ini_set('display_errors', '1'); include_once "Clases/conexion.php"; $usuario = $_POST['usuario']; $password = $_POST['password']; $consulta = "select usu_nombre FROM usu_usuarios WHERE usu_nusuario='$usuario'and usu_password='$password' and usu_activo='SI'"; $mysql_obj = new Conectar(); $mysqli = $mysql_obj->crearConexion(); if($result = $mysqli->query($consulta)) { if ($mysqli->affected_rows > 0) { while($row = $result->fetch_array()) { $myArray[] = $row; } header('Content-type: application/json'); echo json_encode($myArray); }else { header("HTTP/1.0 401 Datos Incorrectos"); header('Content-type: application/json'); $data = array("mensaje" => "Datos Incorrectos"); echo json_encode($data); }} ?>
我正在使用http dart依赖
最后,我使用laravel解决了问题,以这种方式返回了数据
return response()->json($yourData, 200, ['Content-Type' => 'application/json;charset=UTF-8', 'Charset' => 'utf-8'], JSON_UNESCAPED_UNICODE
我注意到,此错误仅发生在模拟器中,而不发生在物理设备中。