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))
Share:
3,351
kenjilaurel
Author by

kenjilaurel

Updated on December 14, 2022

Comments

  • kenjilaurel
    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
      hoangquyy over 4 years
      Is it not working if you use utf8.decode directly to text?
    • kenjilaurel
      kenjilaurel over 4 years
      @hoangquyy it's not working.
    • kenjilaurel
      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
      hoangquyy over 4 years
      This is how I decode utf8: var jsonResponse = convert.jsonDecode(utf8.decode(response.bodyBytes)) with convert is import 'dart:convert' as convert;
    • kenjilaurel
      kenjilaurel over 4 years
      thanks I will try your suggestion @hoangquyy
    • kenjilaurel
      kenjilaurel over 4 years
      @hoangquyy I tried final parsedJson = json.decode(utf8.decode(response.bodyBytes)); but still no luck :(
    • hoangquyy
      hoangquyy over 4 years
      Can you give me your api so I can try?
    • hoangquyy
      hoangquyy over 4 years
      What is the result if you print the string of emoji from api to console?
    • kenjilaurel
      kenjilaurel over 4 years
      @hoangquyy it will display same string I posted above.
    • hoangquyy
      hoangquyy over 4 years
      And you take that string put to Text but it still not display like you want? it's weird
    • kenjilaurel
      kenjilaurel over 4 years
      @hoangquyy yes, but If I copy and paste it to text, it will work.
    • hoangquyy
      hoangquyy over 4 years
      I think you get wrong string
    • kenjilaurel
      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
      hoangquyy over 4 years
      Just try, it's easy to implement. In my case, some font not support my language, so maybe emojis too
    • kenjilaurel
      kenjilaurel over 4 years
      I tried using emoji one font, but still no luck.
  • kenjilaurel
    kenjilaurel over 4 years
    yes, 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
    MrDumb about 3 years
    any solution to this problem ?