某些字符未正确加载

bab*_*aro 2 dart flutter flutter-test flutter-web

下面的示例在我的列表视图中加载数据,但某些字符无效,例如。\xc3\x85 \xc3\x84 我正在尝试使用 utf8

\n\n
var jsonData = json.decode(response.body);\n\nvar jsonData = utf8.decode(response.bodyBytes);\n
Run Code Online (Sandbox Code Playgroud)\n\n

当我使用 utf8 时,结果是正确的,但在 listTile 中加载数据时出现引号并出现错误

\n\n
//I/flutter ( 4629): {"items":[{"name":"xy\xc5\xa1\xc4\x87",  //character is OK but get quotation mark\n//I/flutter ( 4629): {items: [{name: xy\xc3\x84\xc3\x84,  //wrong character\n\n\n\nclass Api {\n  static Future<dynamic> _get(String url) async {\n    try {\n      final response = await http.get(url);\n      var jsonData = json.decode(response.body);\n
Run Code Online (Sandbox Code Playgroud)\n\n

有什么解决办法吗?

\n

Ric*_*eap 7

您的服务器可能没有使用 指定字符集content-type,因此http包默认为Latin-1

将您上面给出的两个部分结合起来。使用 将该字节解码为字符串utf8.decode,然后使用 将该字符串解码为 JSON 到映射json.decode

  var jsonData = json.decode(utf8.decode(response.bodyBytes));
Run Code Online (Sandbox Code Playgroud)