Skip to content

Commit

Permalink
Merge pull request #41 from koji-1009/feat/example_dio
Browse files Browse the repository at this point in the history
feat: Add dio example
  • Loading branch information
koji-1009 authored May 11, 2024
2 parents ae2d905 + bd8ab0e commit c88ffe8
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 38 deletions.
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<TaroHttpResponse> get({
required Uri uri,
required Map<String, String> headers,
}) async {
final response = await dio.getUri<Uint8List>(
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.
Expand Down
38 changes: 38 additions & 0 deletions example/lib/dio_http.dart
Original file line number Diff line number Diff line change
@@ -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<TaroHttpResponse> get({
required Uri uri,
required Map<String, String> headers,
}) async {
final response = await dio.getUri<Uint8List>(
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,
);
}
}
14 changes: 14 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -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());
}
60 changes: 22 additions & 38 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dependencies:
sdk: flutter

cupertino_icons: ^1.0.0
dio: ^5.0.0

taro:
path: ..
Expand Down

0 comments on commit c88ffe8

Please sign in to comment.