put method
Sends an HTTP PUT request to the specified endpoint.
This method is typically used to update an existing resource. It allows for a request body, optional query parameters, and custom headers. Any exceptions encountered during the request will be re-thrown.
To update a user's email address:
try {
final updatedData = {'email': 'new.email@example.com'};
final response = await apiService.put('/users/123', body: updatedData);
print('User updated successfully');
} on DioException catch (e) {
print('Error updating user: $e');
}
Parameters:
endpoint: The URL path or the full URL for the request (e.g., '/users/123').body: The request body with the data to update. Can be aMap,List,String, etc.query: Optional query parameters. Typically aMap<String, dynamic>.customheader: Optional custom headers. Typically aMap<String, String>.
Returns:
- A
Future<Response>that completes with the server's response.
Throws:
- Any
DioException(or other exceptions) that occur during the request.
Implementation
Future<Response> put(String endpoint,
{dynamic body, dynamic query, dynamic customheader}) async {
try {
return await _dio.put(
endpoint,
data: body,
queryParameters: query,
options: Options(headers: customheader),
);
} catch (e) {
rethrow;
}
}