fetchData method

Future<Map<String, dynamic>> fetchData({
  1. int? startTime,
  2. int? endTime,
})

Implementation

Future<Map<String, dynamic>> fetchData({int? startTime, int? endTime}) async {
  String url = baseUrl;
  if (startTime != null || endTime != null) {
    url += "?";
    if (startTime != null) url += "start_time=$startTime";
    if (endTime != null)
      url += "${startTime != null ? '&' : ''}end_time=$endTime";
  }
  print('Fetching data from: $url');
  final response = await http.get(Uri.parse(url));

  if (response.statusCode == 200) {
    final decodedData = json.decode(response.body);
    print('Response body: $decodedData');
    if (decodedData is Map<String, dynamic>) {
      return decodedData;
    } else {
      throw Exception('Unexpected data format: expected a JSON object');
    }
  } else {
    print(
      'Failed to load data, status code: ${response.statusCode}, body: ${response.body}',
    );
    throw Exception(
      'Failed to load data: ${response.statusCode} - ${response.body}',
    );
  }
}