Icare
Icare

Reputation: 1371

_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>' in type cast

I have the error from the Title when trying to do this

  1. I have this kind of json where the first parameter is a unique key

    {
      "3dfb71719a11693760f91f26f4f79c3c": {
        "a": {
          "var1": {
            "value": "8678468,4,2,2,0,0",
            "time": 1544536734000
          },
          "var2": {
            "value": "8678468,4,2,2,0,0",
            "time": 1544536734000
          },
          ...
        },
        "b": {
          "var3": {
            "value": "8678468,4,2,2,0,0",
            "time": 1544536734000
          },
          ...
        },
        "c": {
          "var4": {
            "value": "8678468,4,2,2,0,0",
            "time": 1544536734000
          },
          ...
        }
      },
      "c91891522a8016fc8a097b9e405b118a": {
        "a": {
          ...
        },
        "b": {
          ...
        },
        "c": {
          ...
        }
      }
    }
    
  2. I created @JsonSerialiazable() class to handle the json

    @JsonSeriazable()
    class MyResponse {
      final List<AType> a;
      final List<BType> b;
      final List<Ctype> c;
    
      MyResponse(this.a, this.b, this.c);
    
      factory MyResponse.fromJson(Map<String, dynamic> json) => _$MyResponseFromJson(json);
    }
    
    class AType {
        final StringValueTime var1;
        final StringValueTime var2;
        ...
    
        AType(this.var1, this.var2, ...);
    
        factory AType.fromJson(Map<String, dynamic> json) => $_ATypeFromJson(json);
    }
    
  3. Then, when calling the http.get(), I do this:

    Map<String, dynamic> map = json.decode(response.body);
    List<MyResponse> myObjects = List<MyResponse>();
    
    final keys = map.keys;
    
    keys.forEach((id) {
      final MyResponse obj = MyResponse.fromJson(map[id]);
      myOjects.add(obj);
    });
    

The map variable looks like this:

    0: "3dfb71719a11693760f91f26f4f79c3c"
        key: "3dfb71719a11693760f91f26f4f79c3c"
        value: <Map (3 items)>
          0: "a" <Map x items>
             key: "a"
             value: <Map x items>
          1: "b" <Map x items>
          2: "c" <Map x items>
    1: "c91891522a8016fc8a097b9e405b118a"

When _$MyResponseFromJson(json) is called it gives me the error when it tries to do (json['a'] as List) from my_response.g.dart file generated by the build_runner.

    MyResponse _$MyResponseFromJson(Map<String, dynamic> json) {
      return MyResponse(
          (json['a'] as List)
              ?.map((e) =>
                  e == null ? null : AType.fromJson(e as Map<String, dynamic>))
              ?.toList(),

How can I fix the error?

Thanks

Upvotes: 0

Views: 931

Answers (2)

lrn
lrn

Reputation: 71613

Your a, b and c fields are typed as lists, but the JSON does not have a list at that position, just a single value (as a map).

So, check whether you actually do want the fields to be lists, or whether they should just be single values of the element type.

Upvotes: 0

Icare
Icare

Reputation: 1371

I found my issue with the help of @Irn. The class should be like this

    @JsonSeriazable()
    class MyResponse {
      final AType a;
      final BType b;
      final Ctype c;

      MyResponse(this.a, this.b, this.c);

      factory MyResponse.fromJson(Map<String, dynamic> json) => _$MyResponseFromJson(json);
    }

    class AType {
        final StringValueTime var1;
        final StringValueTime var2;
        ...

        AType(this.var1, this.var2, ...);

        factory AType.fromJson(Map<String, dynamic> json) => $_ATypeFromJson(json);
    }

Upvotes: 1

Related Questions