Converters
Converters are used to apply transformations on request and/or response bodies, for example, transforming a Dart object to a Map<String, dynamic>
or vice versa.
Both converter
and errorConverter
are called before request and response interceptors.
The built-in JSON converter
Chopper provides a JsonConverter
that is able to encode data to JSON and decode JSON strings. It will also apply the correct header to the request (application/json).
However, if content type header is modified (for example by using @Post(headers: {'content-type': '...'})
), JsonConverter
won't add the header and it won't call json.encode if content type is not JSON.
JsonConverter
itself won't convert a Dart object into a Map<String, dynamic>
or a List
, but it will convert a Map<String, dynamic>
into a JSON string.
Implementing custom converters
You can implement custom converters by implementing the Converter
class.
BodyType
is the expected type of the response body (e.g., String
or CustomObject
).
If BodyType
is a List
or a BuiltList
, InnerType
is the type of the generic parameter (e.g., convertResponse<List<CustomObject>, CustomObject>(response)
).
Using different converters for specific endpoints
If you want to apply specific converters only to a single endpoint, you can do so by using the @FactoryConverter
annotation:
Last updated
Was this helpful?