Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart Json UTF-8 Decode

I need to decode the data I receive as utf8. The codes are like this

Future<Products> Get Product() async {
     var response = await http.get(url);
     var decodedJson = json.decode(response.body);
     products = Products.fromJson(decodedJson);
     return products;
   }

I tried the solutions I saw. One of them told me to do it like this

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

When I do this, I get the following

errorException has occurred.
FormatException (FormatException: Missing extension byte (at offset 554))

And it's look like enter image description here

like image 797
Bora Tan Demir Avatar asked Oct 25 '25 09:10

Bora Tan Demir


1 Answers

Back-End Solution

I had the same problem and solved it by setting Character Encoding on the Back-End side of my application.

This can be done by appending the Content-Type Header like this:

Content-Type: application/json;charset=UTF-8

There are different ways to achieve this depending on your Framework/Implementation.

Unfortunately, this answer is not suitable for Flutter per se.

like image 167
Renis1235 Avatar answered Oct 27 '25 01:10

Renis1235