I am trying to get information from two different keys of the response of an API call. Looks like that in the Future call I can return only the response of one key. To explain more, the code below shows the what I am trying to achieve:
Future<PlayerInfo> fetchContestants() async {
final response = await http.get(Uri.parse(URL));
if (response.statusCode == 200) {
String data = response.body;
var playerData = jsonDecode(data)['players'];
var player = playerData.firstWhere(
(element) => element['id'] == 'a8e69669-b4bb-5cb3-9bec-7d860fc080b1');
print(player);
var clubData = jsonDecode(data)['clubs'];
var club =
clubData.firstWhere((element) => element['id'] == player['club_id']);
print(clubData);
return PlayerInfo.fromJson(player);
}
So, I am successfully getting the data that I want, as I can see from the print methods, but I want to return both playerData and clubData at the same return method so I can use this information later on FutureBuilder widget. I was trying to mutate an object like in Javascript, but with Flutter and Dart it gets a bit more complicated according to me, because I need to define the tyepes and because I wanted to do this manually, not using any json serializer package as I am trying to learn the basics of Dart and Flutter. Any way that I could make soemthing like this work?
Here is the PlayerInfo model:
class PlayerInfo {
final String name;
final String surname;
final String id;
final String clubId;
final String position;
final int jerseyNumber;
final String image;
final int totalScore;
final int avgScore;
PlayerInfo(
{required this.name,
required this.surname,
required this.id,
required this.clubId,
required this.position,
required this.jerseyNumber,
required this.image,
required this.totalScore,
required this.avgScore});
factory PlayerInfo.fromJson(Map<String, dynamic> json) {
return PlayerInfo(
name: json['first_name'],
surname: json['last_name'],
id: json['id'],
clubId: json['club_id'],
position: json['position'],
jerseyNumber: json['jersey_number'],
image: json['avatar_urls']['large'],
totalScore: json['total_score'],
avgScore: json['avg_score'],
);
}
}```