3

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": "example@gmail.com",
    "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": "shpra@mail.com",
      "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.

vipin agrahari
  • 2,691
  • 3
  • 21
  • 31

2 Answers2

3

I was also facing the same issue, replace

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

with

standardSerializers.deserializeWith(UserData.serializer, json.decode(response.body))
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
BraveEvidence
  • 53
  • 11
  • 45
  • 119
1

The stack trace could tell you where the error is, so if you have one, it might be useful.

At some point, you are assigning a Map<String, dynamic> value to a variable typed Iterable<dynamic>. That sounds like JSON decoding going wrong, something is assuming a list and getting a map.

There is nothing in the provided code that assumes Iterable<dynamic>, so it's most likely in the json_serializable package that things blow up.

So, are you certain that the JSON input text you have is the correct format for the json_serializable decoder? (I'm not familiar with that package, so I can only guess).

lrn
  • 64,680
  • 7
  • 105
  • 121