├── .gitignore ├── .metadata ├── CHANGELOG.md ├── LICENSE ├── README.md ├── android ├── app │ └── src │ │ └── main │ │ └── java │ │ └── io │ │ └── flutter │ │ └── plugins │ │ └── GeneratedPluginRegistrant.java └── local.properties ├── dio_flutter_transformer.iml ├── example └── example.dart ├── lib └── dio_flutter_transformer.dart ├── pubspec.yaml └── test └── dio_flutter_transformer_test.dart /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .dart_tool/ 3 | 4 | .packages 5 | .pub/ 6 | .idea/ 7 | build/ 8 | ios/.generated/ 9 | ios/Flutter/Generated.xcconfig 10 | ios/Runner/GeneratedPluginRegistrant.* 11 | -------------------------------------------------------------------------------- /.metadata: -------------------------------------------------------------------------------- 1 | # This file tracks properties of this Flutter project. 2 | # Used by Flutter tool to assess capabilities and perform upgrades etc. 3 | # 4 | # This file should be version controlled and should not be manually edited. 5 | 6 | version: 7 | revision: 5391447fae6209bb21a89e6a5a6583cac1af9b4b 8 | channel: beta 9 | 10 | project_type: package 11 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## [3.0.2] - 2019.9.19 2 | 3 | * A dio transformer especially for flutter, by which the json decoding will be in background with [compute] function. 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | TODO: Add your license here. 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # dio_flutter_transformer [![Pub](https://img.shields.io/pub/v/dio_flutter_transformer.svg?style=flat-square)](https://pub.dartlang.org/packages/dio_flutter_transformer) 2 | 3 | > The feature is embedded into the dio package and does not require additional setup. 4 | 5 | A [dio](https://github.com/flutterchina/dio) transformer especially for flutter, by which the json decoding will be in background with [compute] function. 6 | 7 | > Through practical experience, we find that although using `compute` can make tasks go on in the background, it may lead to slow task execution. So please think carefully before using it. 8 | 9 | 10 | ## Install 11 | 12 | ```yaml 13 | dependencies: 14 | dio_flutter_transformer: ^3.0.2 # latest version 15 | ``` 16 | 17 | ## Usage 18 | 19 | Import the package: 20 | 21 | ```dart 22 | import 'package:dio/dio.dart'; 23 | import 'package:dio_flutter_transformer/dio_flutter_transformer.dart'; 24 | ``` 25 | 26 | Then replace dio default transformer: 27 | 28 | ```dart 29 | 30 | var dio=Dio(); 31 | dio.transformer = FlutterTransformer(); // replace dio default transformer 32 | dio.get(...); 33 | ``` 34 | 35 | -------------------------------------------------------------------------------- /android/app/src/main/java/io/flutter/plugins/GeneratedPluginRegistrant.java: -------------------------------------------------------------------------------- 1 | package io.flutter.plugins; 2 | 3 | import io.flutter.plugin.common.PluginRegistry; 4 | 5 | /** 6 | * Generated file. Do not edit. 7 | */ 8 | public final class GeneratedPluginRegistrant { 9 | public static void registerWith(PluginRegistry registry) { 10 | if (alreadyRegisteredWith(registry)) { 11 | return; 12 | } 13 | } 14 | 15 | private static boolean alreadyRegisteredWith(PluginRegistry registry) { 16 | final String key = GeneratedPluginRegistrant.class.getCanonicalName(); 17 | if (registry.hasPlugin(key)) { 18 | return true; 19 | } 20 | registry.registrarFor(key); 21 | return false; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /android/local.properties: -------------------------------------------------------------------------------- 1 | sdk.dir=/Users/duwen/Documents/android_sdk 2 | flutter.sdk=/Users/duwen/Documents/flutter 3 | flutter.versionName=2.0.2 -------------------------------------------------------------------------------- /dio_flutter_transformer.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /example/example.dart: -------------------------------------------------------------------------------- 1 | import 'package:dio/dio.dart'; 2 | import '../lib/dio_flutter_transformer.dart'; 3 | 4 | main() async { 5 | var dio = Dio(); 6 | // replace dio default transformer 7 | dio.transformer = new FlutterTransformer(); 8 | 9 | //.... 10 | Response response = 11 | await dio.get("https://api.github.com/orgs/flutterchina/repos"); 12 | print(response.data); 13 | } 14 | -------------------------------------------------------------------------------- /lib/dio_flutter_transformer.dart: -------------------------------------------------------------------------------- 1 | library dio_flutter_transformer; 2 | 3 | import 'dart:convert'; 4 | import 'package:dio/dio.dart'; 5 | import 'package:flutter/foundation.dart'; 6 | 7 | /// Dio has already implemented a [DefaultTransformer], and as the default 8 | /// [Transformer]. If you want to custom the transformation of 9 | /// request/response data, you can provide a [Transformer] by your self, and 10 | /// replace the [DefaultTransformer] by setting the [dio.Transformer]. 11 | /// 12 | /// [FlutterTransformer] is especially for flutter, by which the json decoding 13 | /// will be in background with [compute] function. 14 | 15 | /// FlutterTransformer 16 | class FlutterTransformer extends DefaultTransformer { 17 | FlutterTransformer() : super(jsonDecodeCallback: _parseJson); 18 | } 19 | 20 | // Must be top-level function 21 | _parseAndDecode(String response) { 22 | return jsonDecode(response); 23 | } 24 | 25 | _parseJson(String text) { 26 | return compute(_parseAndDecode, text); 27 | } 28 | -------------------------------------------------------------------------------- /pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: dio_flutter_transformer 2 | description: A dio transformer espically for flutter, by which the json decoding will be in background with [compute] function in flutter sdk. 3 | 4 | version: 3.0.2 5 | author: wendux <824783146@qq.com> 6 | homepage: https://github.com/flutterchina/dio_flutter_transformer 7 | 8 | environment: 9 | sdk: ">2.4.0 <3.0.0" 10 | 11 | dependencies: 12 | flutter: 13 | sdk: flutter 14 | dio: ^3.0.0 15 | 16 | dev_dependencies: 17 | flutter_test: 18 | sdk: flutter 19 | 20 | # For information on the generic Dart part of this file, see the 21 | # following page: https://www.dartlang.org/tools/pub/pubspec 22 | 23 | # The following section is specific to Flutter. 24 | flutter: 25 | 26 | # To add assets to your package, add an assets section, like this: 27 | # assets: 28 | # - images/a_dot_burr.jpeg 29 | # - images/a_dot_ham.jpeg 30 | # 31 | # For details regarding assets in packages, see 32 | # https://flutter.io/assets-and-images/#from-packages 33 | # 34 | # An image asset can refer to one or more resolution-specific "variants", see 35 | # https://flutter.io/assets-and-images/#resolution-aware. 36 | 37 | # To add custom fonts to your package, add a fonts section here, 38 | # in this "flutter" section. Each entry in this list should have a 39 | # "family" key with the font family name, and a "fonts" key with a 40 | # list giving the asset and other descriptors for the font. For 41 | # example: 42 | # fonts: 43 | # - family: Schyler 44 | # fonts: 45 | # - asset: fonts/Schyler-Regular.ttf 46 | # - asset: fonts/Schyler-Italic.ttf 47 | # style: italic 48 | # - family: Trajan Pro 49 | # fonts: 50 | # - asset: fonts/TrajanPro.ttf 51 | # - asset: fonts/TrajanPro_Bold.ttf 52 | # weight: 700 53 | # 54 | # For details regarding fonts in packages, see 55 | # https://flutter.io/custom-fonts/#from-packages 56 | -------------------------------------------------------------------------------- /test/dio_flutter_transformer_test.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter_test/flutter_test.dart'; 2 | 3 | void main() { 4 | test('adds one to input values', () { 5 | 6 | }); 7 | } 8 | --------------------------------------------------------------------------------