Dart flutter FormatException:无效的 unicode 转义
Dart flutter FormatException: Invalid unicode escape
我在我的项目中使用了 http 包并尝试发送 GET 请求并解码收到 json 但收到错误消息:
E/flutter ( 2292): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: FormatException: Invalid unicode escape (at character 29930)
E/flutter ( 2292): ...\u0438\u043d)"},{"id":374,"title":"\u041\u0435\u0437\u0430\u0431\u0443\u...
这是我的要求:
var response = await http.get(
'${ApiConstants.BASE_URL}$path$formattedParams',
headers: {
'accept': "application/json",
'Authorization': 'Bearer $token',
},
);
return json.decode(response.body);
还尝试使用 json 验证器验证服务器响应,响应在我的例子中是有效的 json。
这是我的 json 响应的一部分,其中捕获错误:
{
"id": 373,
"title": "Настурция (Капуцин)"
},
{
"id": 374,
"title": "Незабудка" // error is here
},
我该如何解决这个问题?
只需以正确的方式从您的服务器发送响应 headers 作为 UTF-8
Headers
{
"Content-Type":"application/json;charset=UTF-8",
"Charset":"utf-8"
}
Laravel 示例:
return response()->json(
$data, // response data
$code, // status code
[
'Content-Type' => 'application/json;charset=UTF-8',
'Charset' => 'utf-8'
],
JSON_UNESCAPED_UNICODE
);
还在 android 清单中的应用程序元素下添加 useCleartextTraffic 属性。
<application
android:usesCleartextTraffic="true"
.....
我在我的项目中使用了 http 包并尝试发送 GET 请求并解码收到 json 但收到错误消息:
E/flutter ( 2292): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: FormatException: Invalid unicode escape (at character 29930)
E/flutter ( 2292): ...\u0438\u043d)"},{"id":374,"title":"\u041\u0435\u0437\u0430\u0431\u0443\u...
这是我的要求:
var response = await http.get(
'${ApiConstants.BASE_URL}$path$formattedParams',
headers: {
'accept': "application/json",
'Authorization': 'Bearer $token',
},
);
return json.decode(response.body);
还尝试使用 json 验证器验证服务器响应,响应在我的例子中是有效的 json。
这是我的 json 响应的一部分,其中捕获错误:
{
"id": 373,
"title": "Настурция (Капуцин)"
},
{
"id": 374,
"title": "Незабудка" // error is here
},
我该如何解决这个问题?
只需以正确的方式从您的服务器发送响应 headers 作为 UTF-8
Headers
{
"Content-Type":"application/json;charset=UTF-8",
"Charset":"utf-8"
}
Laravel 示例:
return response()->json(
$data, // response data
$code, // status code
[
'Content-Type' => 'application/json;charset=UTF-8',
'Charset' => 'utf-8'
],
JSON_UNESCAPED_UNICODE
);
还在 android 清单中的应用程序元素下添加 useCleartextTraffic 属性。
<application
android:usesCleartextTraffic="true"
.....