Dart: FormatException: Unexpected character (at character 1) <!DOCTYPE html> — jsonDecode of an HTML page
jsonDecode received something that is not JSON, commonly an HTML error page, an empty string, or a plain-text message.
FormatException: Unexpected character (at character 1)FormatException: Unexpected end of input (at character 1)FormatException: Unexpected character <!DOCTYPE html>FormatException: Invalid numberUnexpected character (at line 1, character 1) Meaning
The server returned HTML (404/500 page, login redirect, proxy error) but the app tried to parse it as JSON. Check status code and Content-Type, log the body, and confirm the URL. Trailing commas or single quotes also cause Unexpected character.
Common causes
- Server returned HTML for an error or redirect
- Wrong URL or missing API path
- Empty body
- Invalid JSON (single quotes, trailing comma)
- Gzip/encoding problem
⚡ Quick fix
- Print
response.statusCodeandresponse.body - Verify the URL in a browser or curl
- Only decode when status is 200 and Content-Type is JSON
Detailed fix by platform
Dart
if (res.statusCode != 200) throw Exception('HTTP ${res.statusCode}: ${res.body}');
How to diagnose
- Body — HTML or JSON?
- Status — 200?
- URL — Correct?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- Bad state: No element Dart: Bad state: No element — .first, .last or .single on an empty list
- Bad state: Stream has already been listened to Dart: Bad state: Stream has already been listened to
- Cleartext HTTP traffic not permitted Flutter (Android): java.io.IOException: Cleartext HTTP traffic to api.example.com not permitted
Most viewed in Flutter / Dart
- setState() called after dispose() Flutter: setState() called after dispose(): _MyState#1a2b3 (lifecycle state: defunct, not mounted)
- A RenderFlex overflowed Flutter: A RenderFlex overflowed by x pixels on the right/bottom
- Null check operator used on a null value Dart/Flutter: Null check operator used on a null value
Other ways to find it
Where this error sits
Origin: On your device. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 4 Nov 2026