diff --git a/dio/lib/src/dio.dart b/dio/lib/src/dio.dart index a782cad6a..e0a13f3fd 100644 --- a/dio/lib/src/dio.dart +++ b/dio/lib/src/dio.dart @@ -229,9 +229,8 @@ abstract class Dio { /// headers: {HttpHeaders.acceptEncodingHeader: '*'}, // Disable gzip /// ), /// onReceiveProgress: (received, total) { - /// if (total != -1) { - /// print((received / total * 100).toStringAsFixed(0) + '%'); - /// } + /// if (total <= 0) return; + /// print('percentage: ${(received / total * 100).toStringAsFixed(0)}%'); /// }, /// ); /// ``` diff --git a/dio/test/download_test.dart b/dio/test/download_test.dart index 334af34d2..9f341ab14 100644 --- a/dio/test/download_test.dart +++ b/dio/test/download_test.dart @@ -13,14 +13,8 @@ void main() { tearDown(stopServer); test('download1', () async { const savePath = 'test/_download_test.md'; - final dio = Dio(); - dio.options.baseUrl = serverUrl.toString(); - await dio.download( - '/download', savePath, // disable gzip - onReceiveProgress: (received, total) { - // ignore progress - }, - ); + final dio = Dio()..options.baseUrl = serverUrl.toString(); + await dio.download('/download', savePath); final f = File(savePath); expect(f.readAsStringSync(), equals('I am a text file')); @@ -29,11 +23,10 @@ void main() { test('download2', () async { const savePath = 'test/_download_test.md'; - final dio = Dio(); - dio.options.baseUrl = serverUrl.toString(); + final dio = Dio()..options.baseUrl = serverUrl.toString(); await dio.downloadUri( serverUrl.replace(path: '/download'), - (header) => savePath, // disable gzip + (header) => savePath, ); final f = File(savePath); @@ -43,8 +36,7 @@ void main() { test('download error', () async { const savePath = 'test/_download_test.md'; - final dio = Dio(); - dio.options.baseUrl = serverUrl.toString(); + final dio = Dio()..options.baseUrl = serverUrl.toString(); Response response = await dio .download('/error', savePath) .catchError((e) => (e as DioException).response!); @@ -73,7 +65,6 @@ void main() { .catchError((e) => throw (e as DioException).type), throwsA(DioExceptionType.receiveTimeout), ); - //print(r); }); test('download cancellation', () async { diff --git a/dio/test/request_test.dart b/dio/test/request_test.dart index 789985f3a..fd16f2c9e 100644 --- a/dio/test/request_test.dart +++ b/dio/test/request_test.dart @@ -84,12 +84,7 @@ void main() { ); // redirect test - response = await dio.get( - '/redirect', - onReceiveProgress: (received, total) { - // ignore progress - }, - ); + response = await dio.get('/redirect'); expect(response.isRedirect, true); expect(response.redirects.length, 1); final ri = response.redirects.first; diff --git a/dio/test/utils.dart b/dio/test/utils.dart index 0771f0719..6198cde09 100644 --- a/dio/test/utils.dart +++ b/dio/test/utils.dart @@ -96,9 +96,7 @@ Future startServer() async { ..contentLength = content.length ..write(content); - Future.delayed(Duration(milliseconds: 300), () { - response.close(); - }); + Future.delayed(Duration(milliseconds: 300), () => response.close()); return; } diff --git a/example/lib/dio.dart b/example/lib/dio.dart index 999a6e823..352cd7061 100644 --- a/example/lib/dio.dart +++ b/example/lib/dio.dart @@ -46,9 +46,7 @@ void main() async { './example/xx.html', queryParameters: {'a': 1}, onReceiveProgress: (received, total) { - if (total != -1) { - print('$received,$total'); - } + print('received: $received, total: $total'); }, ); diff --git a/example/lib/download.dart b/example/lib/download.dart index ed038d922..1fbbfe62e 100644 --- a/example/lib/download.dart +++ b/example/lib/download.dart @@ -7,6 +7,8 @@ import 'package:dio/dio.dart'; void main() async { final dio = Dio(); dio.interceptors.add(LogInterceptor()); + // Assure the value of total argument of onReceiveProgress is not -1. + dio.options.headers = {HttpHeaders.acceptEncodingHeader: '*'}; final url = 'https://pub.dev/static/hash-rhob5slb/img/pub-dev-logo.svg'; await download1(dio, url, './example/pub-dev-logo.svg'); await download1(dio, url, (headers) => './example/pub-dev-logo-1.svg'); @@ -51,8 +53,7 @@ Future download2(Dio dio, String url, String savePath) async { } } -void showDownloadProgress(received, total) { - if (total != -1) { - print((received / total * 100).toStringAsFixed(0) + '%'); - } +void showDownloadProgress(int received, int total) { + if (total <= 0) return; + print('percentage: ${(received / total * 100).toStringAsFixed(0)}%'); } diff --git a/example/lib/download_with_trunks.dart b/example/lib/download_with_trunks.dart index a43cdc769..c9a727b36 100644 --- a/example/lib/download_with_trunks.dart +++ b/example/lib/download_with_trunks.dart @@ -4,24 +4,20 @@ import 'dart:io'; import 'package:dio/dio.dart'; void main() async { - final url = 'http://download.dcloud.net.cn/HBuilder.9.0.2.macosx_64.dmg'; - final savePath = './example/HBuilder.9.0.2.macosx_64.dmg'; - -// final url = "https://www.baidu.com/img/bdlogo.gif"; -// final savePath = "./example/bg.gif"; + final url = 'https://avatars.githubusercontent.com/u/0'; + final savePath = './example/avatar.png'; await downloadWithChunks( url, savePath, onReceiveProgress: (received, total) { - if (total != -1) { - print('${(received / total * 100).floor()}%'); - } + if (total <= 0) return; + print('${(received / total * 100).floor()}%'); }, ); } -/// Downloading by spiting as file in chunks +/// Downloading by splitting as file in chunks Future downloadWithChunks( url, savePath, { diff --git a/example/lib/formdata.dart b/example/lib/formdata.dart index 44de20ba2..b735b5b84 100644 --- a/example/lib/formdata.dart +++ b/example/lib/formdata.dart @@ -4,12 +4,6 @@ import 'dart:io'; import 'package:dio/dio.dart'; import 'package:dio/io.dart'; -void showProgress(received, total) { - if (total != -1) { - print((received / total * 100).toStringAsFixed(0) + '%'); - } -} - Future formData1() async { return FormData.fromMap({ 'name': 'wendux', @@ -92,7 +86,6 @@ void main() async { final dio = Dio(); dio.options.baseUrl = 'http://localhost:3000/'; dio.interceptors.add(LogInterceptor()); - // dio.interceptors.add(LogInterceptor(requestBody: true)); dio.httpClientAdapter = IOHttpClientAdapter( createHttpClient: () { final client = HttpClient(); @@ -116,13 +109,11 @@ void main() async { print(utf8.decode(await data3.readAsBytes())); response = await dio.post( - //"/upload", 'http://localhost:3000/upload', data: data3, - onSendProgress: (received, total) { - if (total != -1) { - print('${(received / total * 100).toStringAsFixed(0)}%'); - } + onSendProgress: (sent, total) { + if (total <= 0) return; + print('percentage: ${(sent / total * 100).toStringAsFixed(0)}%'); }, ); print(response); diff --git a/example_flutter_app/lib/main.dart b/example_flutter_app/lib/main.dart index 43bb79349..97bc8f2d0 100644 --- a/example_flutter_app/lib/main.dart +++ b/example_flutter_app/lib/main.dart @@ -1,21 +1,9 @@ -import 'dart:convert'; - import 'package:dio/dio.dart'; -import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'http.dart'; // make dio as global top-level variable import 'routes/request.dart'; -// Must be top-level function -_parseAndDecode(String response) { - return jsonDecode(response); -} - -parseJson(String text) { - return compute(_parseAndDecode, text); -} - void main() { dio.interceptors.add(LogInterceptor()); runApp(MyApp()); @@ -91,7 +79,7 @@ class _MyHomePageState extends State { child: SingleChildScrollView( child: Text(_text), ), - ) + ), ], ), ),