Decoding utf8 string in flutter
3,351
If you are using API to get data with emojis.
final response =
await _httpClient.get(uri);
jsonDecode(utf8.decode(response.bodyBytes))
Author by
kenjilaurel
Updated on December 14, 2022Comments
-
kenjilaurel over 1 year
Im trying to display a string with emojis on a text widget. The string is encoded using utf8. My question is how can I decode the whole string so it will display to the text properly?
here is the string with emojis:
Testing emoticons post
\ud83d\ude18\ud83d\udc4c\ud83e\udd70\ud83d\ude02\ud83e\udd73\u2708\ufe0f\ud83d\udc4a\ud83d\ude01\ud83d\ude01\ud83e\udd23\ud83d\ude04\ud83d\ude01\u2708\ufe0f\ud83d\ude01\ud83d\ude0d\ud83d\ude4f\ud83e\udd22\ud83d\ude2d
Currently I have this function :
String utf8convert(String text) { List<int> bytes = text.toString().codeUnits; return utf8.decode(bytes); }
-
hoangquyy over 4 yearsIs it not working if you use utf8.decode directly to text?
-
kenjilaurel over 4 years@hoangquyy it's not working.
-
kenjilaurel over 4 years@hoangquyy if I copy the text and paste it directly to a text widget it is working, but if its coming from the api the emoticons are not decoded.
-
hoangquyy over 4 yearsThis is how I decode utf8: var jsonResponse = convert.jsonDecode(utf8.decode(response.bodyBytes)) with convert is import 'dart:convert' as convert;
-
kenjilaurel over 4 yearsthanks I will try your suggestion @hoangquyy
-
kenjilaurel over 4 years@hoangquyy I tried final parsedJson = json.decode(utf8.decode(response.bodyBytes)); but still no luck :(
-
hoangquyy over 4 yearsCan you give me your api so I can try?
-
hoangquyy over 4 yearsWhat is the result if you print the string of emoji from api to console?
-
kenjilaurel over 4 years@hoangquyy it will display same string I posted above.
-
hoangquyy over 4 yearsAnd you take that string put to Text but it still not display like you want? it's weird
-
kenjilaurel over 4 years@hoangquyy yes, but If I copy and paste it to text, it will work.
-
hoangquyy over 4 yearsI think you get wrong string
-
kenjilaurel over 4 years@hoangquyy no because when I print the text from the api, I will get the same string I posted above. Do you think I need to use font that support emojis?
-
hoangquyy over 4 yearsJust try, it's easy to implement. In my case, some font not support my language, so maybe emojis too
-
kenjilaurel over 4 yearsI tried using emoji one font, but still no luck.
-
-
kenjilaurel over 4 yearsyes, it works if you paste it directly on a Text widget, but if its comming from the api like I posted above, the emojis are not working.
-
MrDumb about 3 yearsany solution to this problem ?