Search code examples
flutterdartdio

type '_OneByteString' is not a subtype of type 'Map<String, dynamic>'


This is an error I get only in release mode (weird) when I'm trying to parse response body of a network request i made into a json map. I put down the code and also the error stacktrace below. the other wierd thing i should mention is that it only happens sometimes. not all the time. but for the apps i have released on the market when i check the crash reports on firebase this error is also reported there. (it does not cause a crash, though)

i should mention i use Dio package as http client

update : I found out that it happens sometimes but when it happens it keeps happening when i send the same request or sometimes other request over and over. until i quit the app and run it again.

Error stacktrace :

I/flutter (31714): type '_OneByteString' is not a subtype of type 'Map<String, dynamic>'
I/flutter (31714): #0      Server.getUser (package:mehrgan/data/network/server.dart:83)
I/flutter (31714): <asynchronous suspension>
I/flutter (31714): #1      Future.wait.<anonymous closure> (dart:async/future.dart:0)
I/flutter (31714): <asynchronous suspension>
I/flutter (31714): type '_OneByteString' is not a subtype of type 'Map<String, dynamic>'
I/flutter (31714): #0      Server.getCourses (package:mehrgan/data/network/server.dart:101)
I/flutter (31714): <asynchronous suspension>
I/flutter (31714): #1      Future.wait.<anonymous closure> (dart:async/future.dart:0)
I/flutter (31714): <asynchronous suspension>

my code :

Response response = await _dio.get('user/info');
Map<String, dynamic> json = jsonDecode(jsonEncode(response.data));

Solution

  • I encountered this problem using Dart/Flutter with the Dio client library.

    Server side I was requesting data from a PHP script.

    I fixed the issue by putting in the following line in my PHP script:

    header('Content-Type: application/json');
    

    Huge thanks to Thiyraash David above who gave me the clue I needed to solve my issue.