[Solved] Unexpected character (at character) Error in Flutter

In this post, we are going to show you how to solve "Unexpected character (at character)" error in Flutter/Dart. This error usually happens while decoding an invalid JSON string, or if you are getting invalid JSON from REST API in the Dio package.

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═════════════════
The following FormatException was thrown building Builder:
Unexpected character (at character 25)

This error happens when you are decoding invalid JSON with json.decode() or jsonDecode().

I/flutter (16776): DioError [DioErrorType.other]: 
FormatException: Unexpected character (at character 1) I/flutter (16776): <br /> I/flutter (16776): ^ I/flutter (16776): I/flutter (16776): Source stack: I/flutter (16776): #0 DioMixin.fetch (package:dio/src/dio_mixin.dart:473:35) I/flutter (16776): #1 DioMixin.request (package:dio/src/dio_mixin.dart:468:12) I/flutter (16776): #2 DioMixin.get (package:dio/src/dio_mixin.dart:55:12)

This error happens when you are getting invalid JSON data from REST API. You need to remind, that Dio() package returns decoded objects from JSON strings. 

The cause of this error is an invalid JSON string, either JSON string inside code lines or JSON string data fetched from REST API.

Case 1: If you are using jsonDecode() or json.decode() to decode the JSON string, you need to verify if the JSON string is invalid. For example:

String jsonstring = '{"error":false,"msg":"",aa"data":[{"name":"Canada","capital":"Ottawa"}]}';
var jsonobj = jsonDecode(jsonstring); // Runtime Error: Unexpected character (at character)

To solve this error let's verify if the JSON string is valid or not. You can use any JSON validator. I'm using https://jsonlint.com/ and I got the error like below when validating the JSON.

Error: Parse error on line 3:
...false,	"msg": "",	aa "data": [{		"nam
---------------------^
Expecting 'STRING', got 'undefined'

Its means, I have an invalid JSON string, for example, there is "aa", an unexpected character after "msg" in the JSON string above code.

You can escape this error with try...catch:

try{
 var jsonobj = jsonDecode(jsonstring);
}catch(e){
  print(e);
}

Case 2: If you are fetching JSON string from REST API, or the internet, you may get an error message generated by the compiler in server-side while generating the JSON string. You can use API tester software to check if the REST API is returning valid JSON, For example:

Here, after checking the response from REST API with API tester software, I got the message like below:

This means, we got the error from the server-side, and the error message is returned by REST API along with JSON string. You can resolve this error on a server-side script or, disable the error message on the compiler.

You can escape this error from Dio() like below:

try{
  response = await dio.get(url);
}catch(e){
  print(e);
}

In this way, you can resolve the "Unexpected character (at character)" Error in Flutter/Dart.

No any Comments on this Article


Please Wait...