From bd8ab0e4de002d46a8e6f3e85599974c8b4d6650 Mon Sep 17 00:00:00 2001 From: Koji Wakamiya Date: Sun, 12 May 2024 01:42:50 +0900 Subject: [PATCH] feat: Add dio example --- README.md | 54 +++++++++++++++++++++++++++++++++++ example/lib/dio_http.dart | 38 +++++++++++++++++++++++++ example/lib/main.dart | 14 +++++++++ example/pubspec.lock | 60 ++++++++++++++------------------------- example/pubspec.yaml | 1 + 5 files changed, 129 insertions(+), 38 deletions(-) create mode 100644 example/lib/dio_http.dart diff --git a/README.md b/README.md index 602a42f..32715a9 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,60 @@ class HomePage extends StatelessWidget { } ``` +### Use another http client + +If you want to use another http client, like [dio](https://pub.dev/packages/dio), you can create a custom `TaroHttpClient`. + +```dart +class DioHttp implements TaroHttpClient { + /// Creates a [DioHttp]. + const DioHttp({ + required this.dio, + }); + + final Dio dio; + + @override + Future get({ + required Uri uri, + required Map headers, + }) async { + final response = await dio.getUri( + uri, + options: Options( + headers: headers, + responseType: ResponseType.bytes, + ), + ); + final data = response.data ?? Uint8List(0); + return ( + statusCode: response.statusCode!, + bodyBytes: data, + reasonPhrase: response.statusMessage, + contentLength: data.length, + headers: response.headers.map.map( + (key, value) => MapEntry(key, value.join(';')), + ), + isRedirect: response.isRedirect, + ); + } +} +``` + +Then, create a `Taro` instance with the custom http client. + +```dart +Taro.instance.networkLoader = TaroLoaderNetwork( + client: DioHttp( + dio: Dio() + ..options.connectTimeout = const Duration(seconds: 10) + ..options.receiveTimeout = const Duration(seconds: 10), + ), +); +``` + + + ## Cache directory If a native cache directory exists, such as Android or iOS, use [path_provider](https://pub.dev/packages/path_provider) to get the `Application Cache directory`. For web, use [Cache API](https://developer.mozilla.org/en-US/docs/Web/API/Cache) to save the cache. diff --git a/example/lib/dio_http.dart b/example/lib/dio_http.dart new file mode 100644 index 0000000..9fe1a52 --- /dev/null +++ b/example/lib/dio_http.dart @@ -0,0 +1,38 @@ +import 'dart:typed_data'; + +import 'package:dio/dio.dart'; +import 'package:taro/taro.dart'; + +class DioHttp implements TaroHttpClient { + /// Creates a [DioHttp]. + const DioHttp({ + required this.dio, + }); + + final Dio dio; + + @override + Future get({ + required Uri uri, + required Map headers, + }) async { + final response = await dio.getUri( + uri, + options: Options( + headers: headers, + responseType: ResponseType.bytes, + ), + ); + final data = response.data ?? Uint8List(0); + return ( + statusCode: response.statusCode!, + bodyBytes: data, + reasonPhrase: response.statusMessage, + contentLength: data.length, + headers: response.headers.map.map( + (key, value) => MapEntry(key, value.join(';')), + ), + isRedirect: response.isRedirect, + ); + } +} diff --git a/example/lib/main.dart b/example/lib/main.dart index 1aed802..1be36de 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,6 +1,20 @@ +import 'package:dio/dio.dart'; import 'package:example/app.dart'; +import 'package:example/dio_http.dart'; import 'package:flutter/material.dart'; +import 'package:taro/taro.dart'; void main() { + const useDio = true; + if (useDio) { + Taro.instance.networkLoader = TaroLoaderNetwork( + client: DioHttp( + dio: Dio() + ..options.connectTimeout = const Duration(seconds: 10) + ..options.receiveTimeout = const Duration(seconds: 10), + ), + ); + } + runApp(const App()); } diff --git a/example/pubspec.lock b/example/pubspec.lock index 5ce66db..a1a71aa 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -5,10 +5,10 @@ packages: dependency: transitive description: name: archive - sha256: "22600aa1e926be775fa5fe7e6894e7fb3df9efda8891c73f70fb3262399a432d" + sha256: ecf4273855368121b1caed0d10d4513c7241dfc813f7d3c8933b36622ae9b265 url: "https://pub.dev" source: hosted - version: "3.4.10" + version: "3.5.1" async: dependency: transitive description: @@ -49,14 +49,6 @@ packages: url: "https://pub.dev" source: hosted version: "1.18.0" - convert: - dependency: transitive - description: - name: convert - sha256: "0f08b14755d163f6e2134cb58222dd25ea2a2ee8a195e53983d57c075324d592" - url: "https://pub.dev" - source: hosted - version: "3.1.1" crypto: dependency: transitive description: @@ -69,10 +61,18 @@ packages: dependency: "direct main" description: name: cupertino_icons - sha256: d57953e10f9f8327ce64a508a355f0b1ec902193f66288e8cb5070e7c47eeb2d + sha256: ba631d1c7f7bef6b729a622b7b752645a2d076dba9976925b8f25725a30e1ee6 url: "https://pub.dev" source: hosted - version: "1.0.6" + version: "1.0.8" + dio: + dependency: "direct main" + description: + name: dio + sha256: "11e40df547d418cc0c4900a9318b26304e665da6fa4755399a9ff9efd09034b5" + url: "https://pub.dev" + source: hosted + version: "5.4.3+1" fake_async: dependency: transitive description: @@ -98,10 +98,10 @@ packages: dependency: "direct dev" description: name: flutter_lints - sha256: e2a421b7e59244faef694ba7b30562e489c2b489866e505074eb005cd7060db7 + sha256: "9e8c3858111da373efc5aa341de011d9bd23e2c5c5e0c62bccf32438e192d7b1" url: "https://pub.dev" source: hosted - version: "3.0.1" + version: "3.0.2" flutter_test: dependency: "direct dev" description: flutter @@ -131,14 +131,6 @@ packages: url: "https://pub.dev" source: hosted version: "4.1.7" - js: - dependency: transitive - description: - name: js - sha256: c1b2e9b5ea78c45e1a0788d29606ba27dc5f71f019f32ca5140f61ef071838cf - url: "https://pub.dev" - source: hosted - version: "0.7.1" leak_tracker: dependency: transitive description: @@ -207,26 +199,26 @@ packages: dependency: transitive description: name: path_provider - sha256: b27217933eeeba8ff24845c34003b003b2b22151de3c908d0e679e8fe1aa078b + sha256: c9e7d3a4cd1410877472158bee69963a4579f78b68c65a2b7d40d1a7a88bb161 url: "https://pub.dev" source: hosted - version: "2.1.2" + version: "2.1.3" path_provider_android: dependency: transitive description: name: path_provider_android - sha256: "477184d672607c0a3bf68fbbf601805f92ef79c82b64b4d6eb318cbca4c48668" + sha256: a248d8146ee5983446bf03ed5ea8f6533129a12b11f12057ad1b4a67a2b3b41d url: "https://pub.dev" source: hosted - version: "2.2.2" + version: "2.2.4" path_provider_foundation: dependency: transitive description: name: path_provider_foundation - sha256: "5a7999be66e000916500be4f15a3633ebceb8302719b47b9cc49ce924125350f" + sha256: f234384a3fdd67f989b4d54a5d73ca2a6c422fa55ae694381ae0f4375cd1ea16 url: "https://pub.dev" source: hosted - version: "2.3.2" + version: "2.4.0" path_provider_linux: dependency: transitive description: @@ -275,14 +267,6 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.8" - pointycastle: - dependency: transitive - description: - name: pointycastle - sha256: "43ac87de6e10afabc85c445745a7b799e04de84cebaa4fd7bf55a5e1e9604d29" - url: "https://pub.dev" - source: hosted - version: "3.7.4" sky_engine: dependency: transitive description: flutter @@ -379,10 +363,10 @@ packages: dependency: transitive description: name: win32 - sha256: "8cb58b45c47dcb42ab3651533626161d6b67a2921917d8d429791f76972b3480" + sha256: "0eaf06e3446824099858367950a813472af675116bf63f008a4c2a75ae13e9cb" url: "https://pub.dev" source: hosted - version: "5.3.0" + version: "5.5.0" xdg_directories: dependency: transitive description: diff --git a/example/pubspec.yaml b/example/pubspec.yaml index dbaa48d..c63181a 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -12,6 +12,7 @@ dependencies: sdk: flutter cupertino_icons: ^1.0.0 + dio: ^5.0.0 taro: path: ..