Flutter chat_ui bad text encoding

Viewed 28

I'm using the package flutter_chat_ui, when sending a word that contains accents, I get a text with special characters.

enter image description here

I looked on the package documentation but I couldn't find how to change the encoding.

My code : I get a list of messages from the server and then display them in the chat.

class MyChatPageState extends State<MyChatPage> {

  List<types.Message> _messages = [];
  final _user = const types.User(id: '82091008');

  // Get messages from the server
  startGetChatMessages() async{
    chatMessages().then((value){
      List result = json.decode(value.body);
      setState(() => _messages = result; );
    }).catchError((onError){
        ...
    });
  }


  @override
  void initState() {
    super.initState();
    startGetChatMessages(); // Get messages
  }


  @override
  Widget build(BuildContext context) {
  return Scaffold(
    body: Chat(
      messages: _messages,
      onSendPressed: _handleSendPressed,
      user: widget.userMe,
      onPreviewDataFetched: _handlePreviewDataFetched,
    );
  }


  // When the user presses the send button
  void _handleSendPressed(types.PartialText message) {
    final textMessage = types.TextMessage(
      author: widget.userMe,
      createdAt: DateTime
          .now()
          .millisecondsSinceEpoch,
      id: randomString(),
      text: message.text,
    );

    _addMessage(textMessage); // send message
  }

}

1 Answers

After several searches I understood that the error came from the encoding of the message retrieved on the server: I should therefore decode the messages in utf8 as indicated by the code below:

I used this:

List result = jsonDecode(utf8.decode(value.bodyBytes));

Instead of this:

List result = json.decode(value.body);

And it works.

Related