httpGetResponse method

Future<Map<String, dynamic>> httpGetResponse({
  1. required String uri,
  2. Map<String, String>? headers,
  3. bool withBaseUrl = true,
})

Implementation

Future<Map<String, dynamic>> httpGetResponse(
    {required String uri,
    Map<String, String>? headers,
    bool withBaseUrl = true}) async {
  try {
    final http.Response response = await http.get(
        Uri.parse(withBaseUrl ? '$baseUrl/$uri' : uri),
        headers: headers);
    if (response.statusCode == 200) {
      return json.decode(response.body);
    }
    throw ApiException(
        statusCode: response.statusCode,
        message: 'Error to load REST API data: ${response.reasonPhrase}');
  } on TimeoutException catch (e) {
    throw ApiException(message: 'Error connection timeout: ${e.message}');
  } on SocketException catch (e) {
    throw ApiException(message: 'Socket error: ${e.message}');
  } on Error catch (e) {
    throw ApiException(message: 'General error: $e');
  }
}