put method

Future<Response> put(
  1. String endpoint, {
  2. dynamic body,
  3. dynamic query,
  4. dynamic customheader,
})

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 a Map, List, String, etc.
  • query: Optional query parameters. Typically a Map<String, dynamic>.
  • customheader: Optional custom headers. Typically a Map<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;
  }
}