put function
This function sends a PUT 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 PUT request to.data
: The data to include in the PUT request.
Returns:
Implementation
Future<Map<String, dynamic>> put(Uri url, dynamic data) async {
try {
final HttpClientRequest req = await HttpClient().putUrl(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};
}
}