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

6,914

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

Share:
6,914
Starrux
Author by

Starrux

Updated on December 11, 2022

Comments

  • Starrux
    Starrux over 1 year

    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