Converting JSON response into correct encoding in Flutter/Dart


It was surprising not finding clear instructions on how to properly display special characters from a JSON response, in Flutter. That's why I'm posting this short article, for future reference.
I had some words with accents such as "café", or "mañana" in my JSON response. Turns out that when Flutter "gets" the response, it uses the default encoding to displays characters. Even if your JSON has the charset in its header, Flutter will fallback to its default encoding, which doesn't support special characters. So how to properly display characters?  Well, get the body bytes, decode it to UTF8, and then convert it. to use it in any way you want. Like this:


Simple as that. This works in Flutter Web, and I really hope it works on Mobile. I haven't tried it.

This concludes converting JSON response into correct encoding in Flutter/Dart. Any questions, feel free to drop a comment below.

No comments

Powered by Blogger.