post function

Future<Map<String, dynamic>> post(
  1. Uri url,
  2. dynamic data
)

This function sends a POST request to the specified URL with the given data and returns the response as a JSON object. If the response is null, it returns an error object with "error: true" field. Otherwise, it returns an object with "error: false" field and the response data.

Parameters:

  • url: The URL to send the POST request to.
  • data: The data to include in the POST request.

Returns:

  • A Future object that will eventually resolve to a Map object.

Implementation

Future<Map<String, dynamic>> post(Uri url, dynamic data) async {
  try {
    final HttpClientRequest req = await HttpClient().postUrl(url);
    req.headers.contentType = ContentType.json;
    req.write(jsonEncode(data));
    final HttpClientResponse res = await req.close();
    final responseBody = await res.transform(utf8.decoder).join();
    final responseJson = jsonDecode(responseBody) as Map<String, dynamic>;
    return {'error': false, ...responseJson};
  } catch (e) {
    return {'error': true, 'errorData': e};
  }
}