flutterdartspecial-charactershttpresponse

Flutter http.Response get wrong character value


I am using the following code to get my data. However, when if my string consists of special symbol/character (), some of the special symbols/character will become some other weird character. I did try to debug code where I form the response and everything works perfectly. But the response body I printed out from flutter returns me the weird character . Can anyone know how could this happen and what I should do to get my expected results?

Example:

Sample & Expected string:

-/:;()£&@.,?!’[]{}#%^*+=_\\|~<>€¥•.,?!’

Output string from response:

-/:;()£&@.,?!’[]{}#%^*+=_\\|~<>€$¥•.,?!’

Sample code on how to call the API:

import 'package:http/http.dart' as http;

var streamedResponse = await request.send()
var response = await http.Response.fromStream(streamedResponse);

Solution

  • The issue you're experiencing is related to character encoding. When you receive the response from the API, the special characters are not being decoded correctly.

    import 'dart:convert';
    
    // Decode the response body using UTF-8 encoding
    var decodedBody = utf8.decode(response.bodyBytes);