getData method

Future<Response> getData({
  1. required String path,
  2. dynamic headers,
})

Implementation

Future<Response> getData({required String path, headers}) async {
  try {
    http.Response response;

    response = await http
        .get(Uri.parse('$url/$apiVer/${path ?? ""}'), headers: headers)
        .timeout(const Duration(milliseconds: 30000));
    print('$url/$apiVer/${path ?? ""}');

    if (response.statusCode == 200) {
      return Response(true, data: jsonDecode(response.body), message: '');
    } else if (response.statusCode == 204) {
      print(response.statusCode);

      return Response(true, message: "No records", data: "No records");
    } else {
      String message = response.body;

      print(response.statusCode);
      print(message);

      return Response(
        false,
        message: response.body,
      );
    }
  } on TimeoutException {
    return Response(false, message: 'timeout', data: "Timeout Exception");
  } on SocketException {
    return Response(false, message: 'socket', data: "Socket Exception");
  } on FormatException catch (e) {
    print(e.toString());
    return Response(false, message: 'format', data: "Invalid Format");
  } on HttpException catch (e) {
    print(e.toString());
    return Response(false, message: 'http', data: "HTTP exception");
  }
}