I'm integrating some API in flutter, but I'm stuck. At one point on API, I need to pass data for the past 15 days, so on that basis I get a result. The issue is the loading time is too long now because I use a for loop and call one API, then the second API, then third and so on. What I'm looking for is can I call my 15 APIs together?
My code
getAllEvent() async {
final storage = new FlutterSecureStorage();
String asset_id = await storage.read(key: "asset_id");
var toDate = DateTime.now();
var fromDate = toDate.subtract(Duration(days: 1));
for (var i = 0; i <= 15; i++) {
// put them into a string
final fromDateString =
'${fromDate.year}-${fromDate.month}-${fromDate.day}';
final toDateString = '${toDate.year}-${toDate.month}-${toDate.day}';
final f = new DateFormat('yyyy-MM-dd');
fromDate = fromDate.subtract(Duration(days: 1));
toDate = toDate.subtract(Duration(days: 1));
final url =
'https://apihide/${asset_id}?start=${f.format(fromDate)}&end=${f.format(toDate)}';
// //print(url);
// //print(ApiToken);
http.Response res = await http.get(
url,
headers: <String, String>{'x-access-token': ApiToken},
);
var data = json.decode(res.body.toString());
if (data['count'] > 0) {
eventList.eventListData.addAll(data['items']);
}
}
setState(() {
eventDataLoaded = true;
checkDataLoaded();
});
}
You can see in code by for loop API is calling one by one. I need to call it 15 times in a single shot. (There is no issue in API if I hit 15 times at once because the API provider says to do this.)