Home > Back-end >  How to handle multiple api calls in flutter
How to handle multiple api calls in flutter

Time:05-07

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]

CodePudding user response:

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;
}
  • Related