如何在Dart中编码Urdu字符串?

时间:2019-04-09 22:19:43

标签: dart

我从网络地址中提取了乌尔都语文本。例如,我的文字是فروردین。但是当我打印它时,我看到“ÙرÙردÛÙ”。如何正确打印?

import 'dart:convert';
import 'package:http/http.dart'; 
import 'package:html/parser.dart'; 
import 'package:html/dom.dart';



Future initiate() async {
  var client = Client();
  Response response = await client.get('https://www.varzesh3.com/');

  var document = parse(response.body);
  List<Element> links = document.querySelectorAll('tr.match-date > td.text-center');

  for (var link in links) {
    print(link.text)
    //var bytes = utf8.encode(link.text);
  }

1 个答案:

答案 0 :(得分:1)

问题似乎是客户端无法识别页面的字符集,并且默认为latin1。请查看以下代码,在其中我强制使用UTF-8而不是将响应作为字节,然后使用utf8解码器将其转换为UTF-8。

import 'dart:convert';
import 'package:http/http.dart';
import 'package:html/parser.dart';
import 'package:html/dom.dart';

main() async {
  var client = Client();
  Response response = await client.get('https://www.varzesh3.com/');

  var document = parse(utf8.decode(response.bodyBytes), encoding: "utf8");
  List<Element> links = document.querySelectorAll(
      'tr.match-date > td.text-center');

  for (var link in links) {
    print(link.text);
  }
}