'How to handle multiple api calls in flutter

I'm trying to call multiple api's but i can't really figure out how to format the results in json for both endpoints.

  Future<List<dynamic>> fetchMedia() async {

  var result = await Future.wait([
  http.get(Uri.parse('https://iptv-org.github.io/api/streams.json')),
  http.get(Uri.parse('https://iptv-org.github.io/api/channels.json')),
  ]);

 return json.decode(result[0].body); 

 }

How can i apply the json.decode() for both result[0] and result[1]



Solution 1:[1]

You have to do it separately, but if you want to return only one list, you can merge them. I don't really see a point in that as the content of files is different.

Future<List<dynamic>> fetchMedia() async {
  final result = await Future.wait([
    http.get(Uri.parse('https://iptv-org.github.io/api/streams.json')),
    http.get(Uri.parse('https://iptv-org.github.io/api/channels.json')),
  ]);
  
  final result1 = json.decode(result[0].body) as List<dynamic>;
  final result2 = json.decode(result[1].body) as List<dynamic>;
  result1.addAll(result2);

  return result1;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user18309290