fetchGames function
Pass cacheKey = "no-cache" to ignore caching
Implementation
Future<GamesResponse> fetchGames(
int limit, int offset, String cachekey, Config config) async {
String path =
'${config.server}/api/tenant/${config.operatorId}/active-games?filter[application]=${config.application}&orderBy=${config.orderBy}&limit=${limit > 0 ? limit : config.limit}&offset=${offset > 0 ? offset : config.offset}&cachekey=$cachekey';
if (cachekey != "no-cache") {
try {
var file = await GameoliveCacheManager.instance
.getSingleFile(path, headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
'Authorization': 'Bearer ${config.token}',
});
if (await file.exists()) {
var res = await file.readAsString();
GamesResponse games = GamesResponse.fromJson(json.decode(res));
// REF_GAMES = games;
return games;
}
} catch (ex) {
print(
"Seems like, there is some trouble with app caching, so fetching from server (a bit unoptimized)");
}
}
final response = await http.get(Uri.parse(path), headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
'Authorization': 'Bearer ${config.token}',
});
if (response.statusCode == 200) {
// If the server did return a 200 OK response,
// then parse the JSON.
var rb = response.body;
// store json data into list
var gameResponse = json.decode(rb) as Map<String, dynamic>;
// iterate over the list and map each object in list to Img by calling Img.fromJson
GamesResponse games = GamesResponse.fromJson(
gameResponse); // list.map((i)=>Game.fromJson(i)).toList();
// print(games.runtimeType); //returns List<Img>
// print(games[0].runtimeType); //returns Img
// REF_GAMES = games;
return games;
} else {
// If the server did not return a 200 OK response,
// then throw an exception.
throw Exception('Failed to load games');
}
}