0

I'm getting back JSON Data from my server but emojis are being serialized with weird characters.

msgcontent: â¤ï¸â¤ï¸â¤ï¸ð,

I'm using the http package. On postman it shows the emoji perfectly so it's not a db or json error. What am I missing?

Thanks.

Leoog
  • 244
  • 2
  • 8
  • Possible duplicate of [Invalid Arabic characters With Utf-8 charset Retrived with http.get Flutter](https://stackoverflow.com/questions/50318681/invalid-arabic-characters-with-utf-8-charset-retrived-with-http-get-flutter) – Richard Heap Jul 30 '19 at 18:25
  • @RichardHeap it's similar to what I was looking for but the wrong answer. Check below for the correct answer. – Leoog Jul 30 '19 at 19:12

4 Answers4

5

Answering my own question after doing some research this is the best and simplest way without using a Future. You can put this anywhere.

import 'dart:convert';

String utf8convert(String text) {
    List<int> bytes = text.toString().codeUnits;
    return utf8.decode(bytes);
}
Boken
  • 4,825
  • 10
  • 32
  • 42
Leoog
  • 244
  • 2
  • 8
2

JUST IN CASE IF SOMEONE IS LOOKING FOR AN EASIER AND CONVENIENT SOLUTION TO THIS VERY PROBLEM

After seeing Leoog's answer, what I would suggest is instead of having a separate function to perform UTF decoding, you can rather simply edit your network request code from something like

final response = await http.get(....);
final result = jsonDecode(response.body);

to

final response = await http.get(....);
final result = jsonDecode(utf8.decode(response.bodyBytes));

This way the UTF decoding would take place inside the data layer keeping our presentation layer neat thus promoting separation of concerns.

Biplab Dutta
  • 444
  • 3
  • 12
0

Try encoding/decoding your data with UTF8 character encoding, this might just do the trick, previously answered:

Flutter UTF8 encoding/decoding answer

E.Bradford
  • 783
  • 7
  • 21
0
    import 'dart:convert' show utf8;
   Text( utf8.decode(receivedString.runes.toList()) );

Try this this should work