Flutter: How to show hindi character in my app?

Viewed 2139

So, i am using API to fetch data and it responds with a string containing Hindi character. But when I printed the response.body it shows as following (gibberish)

enter image description here

Now same is shown on my emulator screen but if I copy pasted the Hindi text and render then it works:

enter image description here

Code snippet of both scenario i.e in title data from API response and in body same string copy pasted( and working as expected):

ListTile(
                        dense: true,
                        contentPadding: EdgeInsets.all(0),
                        title: Text(
                          data['testVersion']['name'] != null
                              ? data['testVersion']['name']
                              : 'NA',
                          style: TextStyle(
                            fontFamily: 'NatoSansDevnagri',
                              fontWeight: FontWeight.w700,
                              fontSize: 18,
                              color: Colors.black),
                        ),
                        trailing: Text(data['testVersion']['time'] != null
                            ? data['testVersion']['time']
                            : 'NA'),
                      ),
                      Text('Below string is copy pasted in text widget from swagger response'),
                      Text(
                      ( 'इस पेपर में 150 वस्तुनिष्ठ बहुविकल्पीय प्रश्न हैं। यह प्रश्न पुस्तिका निम्नलिखित पांच खंडों से बनी है: धारा- I, 11, 111, IV और 30-30 प्रश्न प्रत्येक। उम्मीदवार को सभी वर्गों का प्रयास करना होगा। प्रत्येक प्रश्न एक अंक का है। गलत उत्तर के लिए कोई नकारात्मक अंकन नहीं है'),
                      ),

So, please help me how is it not working from api response ?

Thanks

1 Answers

Dart is probably decoding the server response body in charset Latin-1 (instead of utf-8). You can specify the encoding as :

http.Response response = await http.get('YOUR_API_ENDPOINT',headers: {'Content-Type': 'application/json'});
var responseJson = json.decode(utf8.decode(response.bodyBytes));

print(responseJson) // This should print Hindi characters in the Terminal
Related