Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 534
user9888273 Avatar asked May 13 '19 04:05

user9888273


1 Answers

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);

like image 53
siega Avatar answered Nov 09 '22 03:11

siega