Flutter FormatException:意外字符(在字符1处) [英] Flutter FormatException: Unexpected character (at character 1)
本文介绍了Flutter FormatException:意外字符(在字符1处)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在扑朔迷离中,我使用了一个php文件,该文件从数据库查询返回了json响应,但是当我尝试解码json时,我得到了此错误:
In flutter, i use a php file which returns a json response from a db query, but when i try to decode json i getting this error:
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文件中获取代码:
And code from my php file:
<?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依赖
I'm using http dart dependence
推荐答案
最后,我使用laravel解决了问题,以这种方式返回了数据
Finally i resolve the problem using laravel, returning the data in this way
return response()->json($yourData, 200, ['Content-Type' => 'application/json;charset=UTF-8', 'Charset' => 'utf-8'],
JSON_UNESCAPED_UNICODE
我注意到此错误仅在仿真器中发生,而不在物理设备中发生.
I have noticed that this error occurs only in emulator, but not in a physical device.
这篇关于Flutter FormatException:意外字符(在字符1处)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文