0

I am trying to parse data from a json. I think this image will help you to understand the problem.

enter image description here

I am getting Exception has occurred. _TypeError (type '(dynamic) => MainRank' is not a subtype of type '(String, dynamic) => MapEntry<dynamic, dynamic>' of 'transform')

If you hover over the body, you will see the black pop up with the data. ranks is a list and there are two extra properties there as well.

My MainRank class is like this

class MainRank{
  String divisionName;
  String tournamentName;

  final  List<Ranks> ranks; 

  MainRank(this.divisionName, this.tournamentName, this.ranks);
  factory MainRank.fromJson(Map<String, dynamic> json) =>
     _$MainRankFromJson(json);
  Map<String, dynamic> toJson() => _$MainRankToJson(this);
}

Please help me with a little bit explanation how it works in dart. I am from php/js background so data types seems giving me hard time :) Thank you,

EDIT Response from my api is

{
  ranks: [....],
  divisionName: "Division 2",
  tournamentName: "Season 4"
}

And code I am using to parse the json is

Future _getData() async{
var res = await CallApi().getData('standings/${widget.id}');
final body = json.decode(res.body);
// final matches = body['matches'];
var data;

if(body!=null){
   data = body.map((el) => MainRank.fromJson(el));
}
print(data);
 return null;
}
4
  • 1
    what is the json response from your webservice ? Commented Apr 10, 2019 at 19:44
  • Edited the question. Please see :) Thank you Commented Apr 10, 2019 at 19:47
  • 1
    what is the line of your error? Commented Apr 10, 2019 at 19:47
  • This is the line data = body.map((el) => MainRank.fromJson(el)); Commented Apr 10, 2019 at 19:48

1 Answer 1

1

You want to get the MainRank from your json data, so :

Change this :

 var data;

 if(body!=null){
    data = body.map((el) => MainRank.fromJson(el));
 }

To this:

MainRank data;

if(body!=null){
    data = MainRank.fromJson(body);
 }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. It worked. I was used to parsing list so I had to use map. :) now it all works nicely!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.