post method

Future<Map<String, dynamic>> post(
  1. String url, {
  2. Map<String, dynamic>? data,
  3. Map<String, dynamic>? header,
})

Implementation

Future<Map<String, dynamic>> post(String url,
    {Map<String, dynamic>? data, Map<String, dynamic>? header}) async {
  // Await the http get response, then decode the json-formatted response.
  http.Response response;
  data?.length;
  Map<String, String> headers = Map<String, String>();
  header?.forEach((k, v) => headers[k] = v);
  if (data!=null && data.isNotEmpty) {
    headers["content-length"] = json.encode(data).length.toString();
    print('headers--> ${headers.toString()}');
    response =
        await http.post(Uri.parse(url), body: json.encode(data), headers: headers);
  } else {
    response = await http.post(Uri.parse(url), headers: headers);
  }
  if (response.statusCode == 200 || response.statusCode == 201) {
    var jsonResponse = json.decode(response.body);
    print('Response: $jsonResponse.');
    return jsonResponse;
  } else {
    print('Request failed with status: ${response.statusCode}.');
    return Map();
  }
}