Leoog
Leoog

Reputation: 244

Flutter Text Widget Emojis

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.

Upvotes: 0

Views: 4371

Answers (4)

Rabin Acharya
Rabin Acharya

Reputation: 78

    import 'dart:convert' show utf8;
   Text( utf8.decode(receivedString.runes.toList()) );

Try this this should work

Upvotes: 0

Biplab Dutta
Biplab Dutta

Reputation: 494

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.

Upvotes: 2

Leoog
Leoog

Reputation: 244

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

Upvotes: 5

E.Bradford
E.Bradford

Reputation: 813

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

Flutter UTF8 encoding/decoding answer

Upvotes: 0

Related Questions