postRequest method

Future postRequest(
  1. String url, {
  2. Map<String, String>? headers,
  3. dynamic body,
  4. dynamic encoding,
})

Sends a POST request to the specified URL and returns the response

Implementation

Future<dynamic> postRequest(String url,
    {Map<String, String>? headers, body, encoding}) async {
  var val = await ConnectivityService().checkConnectivity();
  if (val) {
    return http
        .post(Uri.parse(url),
            body: json.encode(body), headers: headers, encoding: encoding)
        .then((http.Response response) {
      final statusCode = response.statusCode;
      if (statusCode == 200) {
        return {
          'status': true,
          'body': utf8.decode(response.bodyBytes),
          'message': 'success',
          'header': response.headers,
          'code': statusCode,
        };
      } else if (statusCode == 201) {
        return {
          'status': true,
          'body': response.body,
          'message': 'created',
          'header': response.headers,
          'code': statusCode,
        };
      } else {
        return {
          'status': false,
          'body': response.body,
          'message': (response.statusCode == 404)
              ? 'Page not Found'
              : (response.statusCode == 401)
                  ? 'Unauthorized'
                  : 'Error occured while Fetching Data',
          'header': response.headers,
          'code': statusCode,
        };
      }
    });
  } else {
    return {
      'status': false,
      'message': 'No Internet',
    };
  }
}