-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from koji-1009/feat/example_dio
feat: Add dio example
- Loading branch information
Showing
5 changed files
with
129 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ dependencies: | |
sdk: flutter | ||
|
||
cupertino_icons: ^1.0.0 | ||
dio: ^5.0.0 | ||
|
||
taro: | ||
path: .. | ||
|