Flutter FormatException: Unexpected character (at character 1)

Last updated on November 6, 2020 Guest Contributor Loading... One comment

Problem

When sending a request in Flutter, you may want to decode the JSON response. When doing that, you may face the following error:

Flutter FormatException: Unexpected character (at character 1)

This error is caused by the wrong JSON format you get from the response body (e.g. it doesn’t start with an open curly bracket “{“).

Solutions

Printing out the response.body, see the output, and try one of the following solutions:

1. Correct the API endpoint if there is something wrong with it.

2. Provide header information when sending request:

{
    'Content-Type': 'application/json',
    'Charset': 'utf-8'
}

3. The API may need some auth credentials like username, password, token… Provide them if need.

4. Maybe the bug isn’t in your Flutter code but on the server side. Review it.

Subscribe
Notify of
guest
1 Comment
Inline Feedbacks
View all comments
Hyefur
Hyefur
4 months ago

Please elaborate more on how to solve this

Related Articles