How to decode utf-8 from REST api in Dart code?

点点圈 提交于 2020-07-09 07:19:06

问题


I am trying to create a news report app from a Wordpress site using REST API but it is encoded in UTF-8.

I am new in Dart, all I know is that I can decode Utf-8 just by an array of characters not by giving a string (as an input). Then how can I decode a string in Utf-8 for my app?

child: new Text(posts[index]["title"]['rendered'],
                                style: TextStyle(
                                  fontSize: 21,
                                  fontWeight: FontWeight.bold,
                                ),
                                textAlign: TextAlign.center),
                          ),
                          new Padding(
                            padding: EdgeInsets.all(10.0),
                            child: new ListTile(
                              subtitle: new Text(posts[index]["excerpt"]
                                      ["rendered"]
                                  .replaceAll(new RegExp(r'<[^>]*>'), '')),
                            ),
                          ),

Here is a picture of the result: https://imgur.com/gallery/qxkc9yP


回答1:


You can get the body bytes and convert it..

 http.Response response = await _api.getData();
 String source = Utf8Decoder().convert(response.bodyBytes);

 // Convert to your class instance...
 MyClass instance = json.decode(source);



来源:https://stackoverflow.com/questions/56105646/how-to-decode-utf-8-from-rest-api-in-dart-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!