Skip to content

Commit

Permalink
Transformer documentation updates (cfug#2190)
Browse files Browse the repository at this point in the history
Updated the documentation to reflect that `BackgroundTransformer` is the
new default transformer.

- added information about BackgroundTransformer
- removed the outdated & deprecated code example
- removed outdated info about methods (only applies to transformRequest,
as explained in next paragraph)
- updated TOC

### New Pull Request Checklist

- [x] I have read the
[Documentation](https://pub.dev/documentation/dio/latest/)
- [x] I have searched for a similar pull request in the
[project](https://github.com/cfug/dio/pulls) and found none
- [x] I have updated this branch with the latest `main` branch to avoid
conflicts (via merge from master or rebase)
- [ ] I have added the required tests to prove the fix/feature I'm
adding
- [x] I have updated the documentation (if necessary)
- [ ] I have run the tests without failures
- [ ] I have updated the `CHANGELOG.md` in the corresponding package
  • Loading branch information
bbjay authored Apr 17, 2024
1 parent 0364fe6 commit 2d2cdc4
Showing 1 changed file with 5 additions and 28 deletions.
33 changes: 5 additions & 28 deletions dio/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ Timeout, Custom adapters, Transformers, etc.
* [Multiple files upload](#multiple-files-upload)
* [Reuse `FormData`s and `MultipartFile`s](#reuse-formdatas-and-multipartfiles)
* [Transformer](#transformer)
* [In Flutter](#in-flutter)
* [Other example](#other-example)
* [Transformer example](#transformer-example)
* [HttpClientAdapter](#httpclientadapter)
* [Using proxy](#using-proxy)
* [HTTPS certificate verification](#https-certificate-verification)
Expand Down Expand Up @@ -724,39 +723,17 @@ Future<void> _repeatedlyRequest() async {

`Transformer` allows changes to the request/response data
before it is sent/received to/from the server.
This is only applicable for request methods 'PUT', 'POST', and 'PATCH'.
Dio has already implemented a `DefaultTransformer` as default.
Dio has already implemented a `BackgroundTransformer` as default,
which calls `jsonDecode` in an isolate if the response is larger than 50 KB.
If you want to customize the transformation of request/response data,
you can provide a `Transformer` by your self,
and replace the `DefaultTransformer` by setting the `dio.transformer`.
and replace the `BackgroundTransformer` by setting the `dio.transformer`.

> `Transformer.transformRequest` only takes effect when request with `PUT`/`POST`/`PATCH`,
> they're methods that can contain the request body.
> `Transformer.transformResponse` however, can be applied to all types of responses.
### In Flutter

If you're using Dio in Flutter development,
it's better to decode JSON in isolates with the `compute` function.

```dart
/// Must be top-level function
Map<String, dynamic> _parseAndDecode(String response) {
return jsonDecode(response) as Map<String, dynamic>;
}
Future<Map<String, dynamic>> parseJson(String text) {
return compute(_parseAndDecode, text);
}
void main() {
// Custom `jsonDecodeCallback`.
dio.transformer = DefaultTransformer()..jsonDecodeCallback = parseJson;
runApp(MyApp());
}
```

### Other example
### Transformer example

There is an example for [customizing Transformer](../example/lib/transformer.dart).

Expand Down

0 comments on commit 2d2cdc4

Please sign in to comment.