【问题标题】:Some of character is not load correctly某些字符未正确加载
【发布时间】:2020-04-07 08:28:32
【问题描述】:

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

var jsonData = json.decode(response.body);

var jsonData = utf8.decode(response.bodyBytes);

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

//I/flutter ( 4629): {"items":[{"name":"xyšć",  //character is OK but get quotation mark
//I/flutter ( 4629): {items: [{name: xyÄÄ,  //wrong character



class Api {
  static Future<dynamic> _get(String url) async {
    try {
      final response = await http.get(url);
      var jsonData = json.decode(response.body);

有什么办法吗?

【问题讨论】:

  • 顺便说一句,创建仅包含静态方法的类(如您的Api)并不被认为是好的 Dart 风格。只需将它们的函数设为顶级函数即可。

标签: flutter dart flutter-test flutter-web


【解决方案1】:

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

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

  var jsonData = json.decode(utf8.decode(response.bodyBytes));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-29
    • 1970-01-01
    • 2012-03-27
    • 1970-01-01
    • 1970-01-01
    • 2014-03-08
    相关资源
    最近更新 更多