FormatException: Unexpected character Flutter / Dart Origin: On your device OSI layer 7 · Application Last updated 4 Nov 2026

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.

Exact message you may see 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)
Seen on: Flutter

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

  1. Print response.statusCode and response.body
  2. Verify the URL in a browser or curl
  3. Only decode when status is 200 and Content-Type is JSON

Detailed fix by platform

Dart

  1. if (res.statusCode != 200) throw Exception('HTTP ${res.statusCode}: ${res.body}');

How to diagnose

  1. Body — HTML or JSON?
  2. Status — 200?
  3. 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:

Where this error sits

Origin: On your device. Errors that are often confused with or caused by this one (click to open):

Bad state: No element — Dart: Bad state: No element — .first, .last or .single on an empty list (Flutter / Dart)Bad state: No elementBad state: Stream has already been listened to — Dart: Bad state: Stream has already been listened to (Flutter / Dart)Bad state: Stream has…Cleartext HTTP traffic not permitted — Flutter (Android): java.io.IOException: Cleartext HTTP traffic to api.example.com not permitted (Flutter / Dart)Cleartext HTTP traffi…Error: Couldn't resolve the package — Dart: Error: Couldn't resolve the package 'x' in 'package:x/x.dart' (Flutter / Dart)Error: Couldn't resol…Illegal argument in isolate message: object is unsendable — Dart: Invalid argument(s): Illegal argument in isolate message: object is unsendable - Library:'dart:async' Class: _Future@4048458 (Flutter / Dart)Illegal argument in i…RangeError (index): Invalid value — Dart: RangeError (index): Invalid value: Valid value range is empty: 0 (Flutter / Dart)RangeError (index): I…SocketException: Failed host lookup — Flutter: Unhandled Exception: SocketException: Failed host lookup: 'api.example.com' (OS Error: No address associated with hostname, errno = 7) (Flutter / Dart)SocketException: Fail…The argument type can't be assigned to the parameter type — Dart: The argument type 'String?' can't be assigned to the parameter type 'String' (Flutter / Dart)The argument type can…FormatExc…

Explore the map of this error Full error map →

🧠 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.