Home > Enterprise >  Json decode and 'Inline Local Variable' is resulting in _TypeError
Json decode and 'Inline Local Variable' is resulting in _TypeError

Time:12-21

I am parsing a JSON array to an Iterable<Dto> such as:

final Iterable l = json.decode(resp.body);
Iterable<Dto> dtos = l.map((data) => Dto.fromJson(data));

But it results in _TypeError (type 'MappedListIterable<dynamic, dynamic>' is not a subtype of type 'Iterable<Dto>') when we chain the two statements in:

Iterable<Dto> dtos = json
                       .decode(resp.body)
                       .map((data) => Dto.fromJson(data));

Notice, this is the result of the refactor operation Inline Local Variable.

So, how should we properly refactor with 'Inline Local Variable'?

CodePudding user response:

You can try below lines;

Iterable<Dto> dtos = (json.decode(resp.body) as Iterable)
                       .map((data) => Dto.fromJson(data));

CodePudding user response:

you can also get them as List:

var l = json.decode(resp.body);

List<Dto> dtos = l.map((data) => Dto.fromJson(data)).toList();

  • Related