Andreas Hunter
Andreas Hunter

Reputation: 5014

Dart flutter FormatException: Invalid unicode escape

I use http package in my project and tried send GET request and decode recived json but get error with message:

E/flutter ( 2292): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: FormatException: Invalid unicode escape (at character 29930)
E/flutter ( 2292): ...\u0438\u043d)"},{"id":374,"title":"\u041\u0435\u0437\u0430\u0431\u0443\u...

Here is my request:

var response = await http.get(
  '${ApiConstants.BASE_URL}$path$formattedParams',
  headers: {
    'accept': "application/json",
    'Authorization': 'Bearer $token',
  },
);
return json.decode(response.body);

Also tried validate server response using json validators and response is valid json in my case.

Here is part of my json response where catch error:

{
    "id": 373,
    "title": "Настурция (Капуцин)"
},
{
    "id": 374,
    "title": "Незабудка" // error is here
},

How I can fix this problem?

Upvotes: 0

Views: 1332

Answers (1)

Otabek Mansurov
Otabek Mansurov

Reputation: 218

Just send response headers from your server in right way as UTF-8

Headers

{
  "Content-Type":"application/json;charset=UTF-8",
  "Charset":"utf-8"
}

Laravel example:

return response()->json(
    $data, // response data
    $code, // status code
    [
        'Content-Type' => 'application/json;charset=UTF-8', 
        'Charset' => 'utf-8'
    ], 
    JSON_UNESCAPED_UNICODE
);

Also add useCleartextTraffic attribute under application element in android manifest.

<application
    android:usesCleartextTraffic="true"
    .....

Upvotes: 2

Related Questions