markgravity / object_mapper

Object Mapper for Flutter
8 stars 3 forks source link

How to convert Json list into List<Model> #1

Closed matheuscuba closed 4 years ago

matheuscuba commented 4 years ago

When I try to convert a json list into List<MyModel> its throw a error:

Error: Expected a value of type 'Map<String, dynamic>', but got one of type 'List'

How can I convert a json like this?

[
      {
         "id":"ca25b59c-e4a2-4e8d-966d-5638f22f2d4d",
         "name":"Sequências",
         "type":1,
         "image":"24324a60-b8fc-4905-8f44-bbde6c7cc6e0.png"
      },
     {
         "id":"ca25b59c-e4a2-4e8d-966d-5638f22f2d4d",
         "name":"Sequências",
         "type":1,
         "image":"24324a60-b8fc-4905-8f44-bbde6c7cc6e0.png"
      },

]
markgravity commented 4 years ago

Hi, I believe you should define in the mapping function like this:

class TheList {

   List<MyModel> items;

  void mapping(Mapper map) {
     map<MyModel>("items", items, (v) => items = v);
  }
}
matheuscuba commented 4 years ago

Hi, I believe you should define in the mapping function like this:

class TheList {

   List<MyModel> items;

  void mapping(Mapper map) {
     map<MyModel>("items", items, (v) => items = v);
  }
}

Thanks, but didn't work. I ended up using a workaround, by changing the api to return the list in a property like this:

{
    list: [  ]
}

so the library can map my model.

I will be closing the issue