Invalid Arabic characters With Utf-8 charset Retrived with http.get Flutter

前端 未结 1 625
孤独总比滥情好
孤独总比滥情好 2020-12-03 10:03

Hi i\'m trying to Fetch data from the internet in flutter and as long as all characters in response.body are English everything is fine but i get t

相关标签:
1条回答
  • 2020-12-03 10:48

    The web server's Content-Type header is Content-Type: text/html. Note that isn't including a charset suffix. It should be saying Content-Type: text/html; charset=utf-8. The package:http client looks for this charset when asked to decode to characters. If it's missing it defaults to LATIN1 (not utf-8).

    As you've seen, setting the headers on the Request doesn't help, as it's the Response that does the decoding. Luckily, there's a simple fix. Just decode the bytes to String yourself like this.

    Future<String> loadFarsi() async {
      final response =
          await http.get("http://mobagym.com/media/mobagym-app-info/farsi.html");
      String body = utf8.decode(response.bodyBytes);
      print(body);
      return body;
    }
    
    0 讨论(0)
提交回复
热议问题