Json_serializable.dart: failed due to: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Iterable<dynamic>' in type cast

Created on 9 Oct 2018  路  5Comments  路  Source: google/json_serializable.dart

I am trying to use built_value and json_serializable together for parsing json response coming from server to model classes.

Following are the dependencies:

  built_collection: ^4.0.0
  built_value: ^6.1.4
dev_dependencies:
  build_runner: ^1.0.0
  built_value_generator: ^6.1.4
  json_serializable: ^1.4.0

Following is the code that I have written

abstract class UserData implements Built<UserData, UserDataBuilder>{

  String get user_first_name;
  String get user_last_name;
  String get user_mobile;
  String get email;
  String get user_type;
  Company get company;
  UserType get type;

  UserData._();
  factory UserData([updates(UserDataBuilder b)]) = _$UserData;
  static Serializer<UserData> get serializer => _$userDataSerializer;

}

abstract class Company implements Built<Company, CompanyBuilder>{

  String get id;

  Company._();
  factory Company([updates(CompanyBuilder b)]) = _$Company;
  static Serializer<Company> get serializer => _$companySerializer;


}


abstract class UserType implements Built<UserType, UserTypeBuilder>{

  String get id;
  UserType._();
  factory UserType([updates(UserTypeBuilder b)]) = _$UserType;
  static Serializer<UserType> get serializer => _$userTypeSerializer;


}


Serializers class code :

@SerializersFor(const [
  UserData
])

Serializers serializers = _$serializers;

Serializers standardSerializers =
(serializers.toBuilder()..addPlugin(StandardJsonPlugin())).build();

Following is the response that I am getting from server.

{
  "user": {
    "id": "505d27b0-acaa-11e8-b916-21359608417b",
    "email": "[email protected]",
    "user_first_name": "Pankaj",
    "user_last_name": "P",
    "user_dob": null,
    "active_status": 1,
    "user_region_id": null,
    "user_base_currency": "USD",
    "user_address": null,
    "is_god_user": 0,
    "is_super_user": 0,
    "profile": null,
    "advanced_search": 0,
    "region": null,
    "company": {
      "id": "24e311f0-acaa-11e8-8750-8de299c7797b",
      "company_name": "SHPR A",
      "company_address": null,
      "company_logo": "",
      "company_constitution": "pvt_ltd",
      "company_email": "[email protected]",
      "state": null,
      "country": null,
      "postal_code": null,
      "date_of_establishment": null,
      "number_of_employees": null,
      "company_turnover": null,
      "vendor_id": null
    },
    "type": {
      "id": "5eeebe55-fdf4-11e7-81f1-ac7ba173bed6",
      "user_type_code": "11",
      "user_type_name": "ADMIN",
      "user_category": "SHIPPER"
    }
  }
}

Finally I am trying to parse using the following line of code

serializers.deserializeWith(UserData.serializer, json.decode(response.body))

However I am getting following error

failed due to: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Iterable<dynamic>' in type cast

Please throw some light into what might be causing this issue.

Most helpful comment

Try replacing

serializers.deserializeWith(UserData.serializer, json.decode(response.body))

with

standardSerializers.deserializeWith(UserData.serializer, json.decode(response.body))

All 5 comments

Every class you list here implements Built, but is not annotated w/ @JsonSerializable.

This feel like an issue with built_value

Is there a specific reason you're mixing json_serializable with built_value?
They have a big overlap. Perhaps you can/should decide for one of these.

Closing this out 鈥撀爈et me know if you have more information.

Try replacing

serializers.deserializeWith(UserData.serializer, json.decode(response.body))

with

standardSerializers.deserializeWith(UserData.serializer, json.decode(response.body))

Was this page helpful?
0 / 5 - 0 ratings

Related issues

matanlurey picture matanlurey  路  5Comments

AlexBigCheese picture AlexBigCheese  路  3Comments

fzyzcjy picture fzyzcjy  路  5Comments

enyo picture enyo  路  5Comments

xiang23 picture xiang23  路  5Comments