Skip to content
This repository was archived by the owner on Jul 6, 2020. It is now read-only.

Commit f9b4402

Browse files
author
mchome
committed
1. update deps
2. support range fetch when retry 3. add skipRetryStatusCode, fix #107
1 parent 31953b3 commit f9b4402

File tree

6 files changed

+44
-19
lines changed

6 files changed

+44
-19
lines changed

example/lib/main.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class LoadImage extends StatelessWidget {
8888
color: Colors.transparent,
8989
child: const Icon(Icons.refresh),
9090
),
91-
imageFilter: ImageFilter.blur(sigmaX: 10.0, sigmaY: 10.0),
91+
// imageFilter: ImageFilter.blur(sigmaX: 10.0, sigmaY: 10.0),
9292
width: 300.0,
9393
height: 300.0,
9494
enableRefresh: true,

lib/src/provider/flutter_advanced_networkimage.dart

+5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class AdvancedNetworkImage extends ImageProvider<AdvancedNetworkImage> {
3636
this.postProcessing,
3737
this.disableMemoryCache: false,
3838
this.printError = false,
39+
this.skipRetryStatusCode,
3940
}) : assert(url != null),
4041
assert(scale != null),
4142
assert(useDiskCache != null),
@@ -118,6 +119,9 @@ class AdvancedNetworkImage extends ImageProvider<AdvancedNetworkImage> {
118119
/// Print error messages.
119120
final bool printError;
120121

122+
/// The [HttpStatus] code that you can skip retrying if you meet them.
123+
final List<int> skipRetryStatusCode;
124+
121125
ImageStream resolve(ImageConfiguration configuration) {
122126
assert(configuration != null);
123127
final ImageStream stream = ImageStream();
@@ -262,6 +266,7 @@ Future<Uint8List> _loadFromDiskCache(
262266
key.timeoutDuration,
263267
key.loadingProgress,
264268
key.getRealUrl,
269+
skipRetryStatusCode: key.skipRetryStatusCode,
265270
printError: key.printError,
266271
);
267272
if (imageData != null) {

lib/src/provider/flutter_advanced_networksvg.dart

+6-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class AdvancedNetworkSvg extends PictureProvider<AdvancedNetworkSvg> {
3333
this.cacheRule,
3434
this.getRealUrl,
3535
this.printError = false,
36+
this.skipRetryStatusCode,
3637
}) : assert(url != null),
3738
assert(scale != null),
3839
assert(useDiskCache != null),
@@ -89,9 +90,12 @@ class AdvancedNetworkSvg extends PictureProvider<AdvancedNetworkSvg> {
8990
/// Extract the real url before fetching.
9091
final UrlResolver getRealUrl;
9192

92-
/// Print error.
93+
/// Print error messages.
9394
final bool printError;
9495

96+
/// The [HttpStatus] code that you can skip retrying if you meet them.
97+
final List<int> skipRetryStatusCode;
98+
9599
@override
96100
Future<AdvancedNetworkSvg> obtainKey(PictureConfiguration picture) {
97101
return SynchronousFuture<AdvancedNetworkSvg>(this);
@@ -222,6 +226,7 @@ Future<Uint8List> _loadFromDiskCache(AdvancedNetworkSvg key, String uId) async {
222226
key.timeoutDuration,
223227
null,
224228
key.getRealUrl,
229+
skipRetryStatusCode: key.skipRetryStatusCode,
225230
printError: key.printError,
226231
);
227232
if (imageData != null) {

lib/src/utils.dart

+21-6
Original file line numberDiff line numberDiff line change
@@ -304,25 +304,32 @@ Future<Uint8List> loadFromRemote(
304304
Duration timeoutDuration,
305305
LoadingProgress loadingProgress,
306306
UrlResolver getRealUrl, {
307+
List<int> skipRetryStatusCode,
307308
bool printError = false,
308309
}) async {
309310
assert(url != null);
310311
assert(retryLimit != null);
311312

312313
if (retryLimit < 0) retryLimit = 0;
314+
skipRetryStatusCode ??= [];
313315

314316
/// Retry mechanism.
315317
Future<http.Response> run<T>(Future f(), int retryLimit,
316318
Duration retryDuration, double retryDurationFactor) async {
317319
for (int t in List.generate(retryLimit + 1, (int t) => t + 1)) {
318320
try {
319321
http.Response res = await f();
320-
if (res != null && res.bodyBytes.length > 0) {
321-
if (res.statusCode == HttpStatus.ok)
322+
if (res != null) {
323+
if ([HttpStatus.ok, HttpStatus.partialContent]
324+
.contains(res.statusCode) &&
325+
res.bodyBytes.length > 0) {
322326
return res;
323-
else if (printError)
324-
debugPrint('Load error, response status code: ' +
325-
res.statusCode.toString());
327+
} else {
328+
if (printError)
329+
debugPrint(
330+
'Failed to load, response status code: ${res.statusCode.toString()}.');
331+
if (skipRetryStatusCode.contains(res.statusCode)) return null;
332+
}
326333
}
327334
} catch (e) {
328335
if (printError) debugPrint(e.toString());
@@ -334,6 +341,9 @@ Future<Uint8List> loadFromRemote(
334341
return null;
335342
}
336343

344+
List<int> buffer = [];
345+
bool acceptRangesHeader = false;
346+
337347
http.Response _response;
338348
_response = await run(() async {
339349
String _url = url;
@@ -342,8 +352,13 @@ Future<Uint8List> loadFromRemote(
342352
if (loadingProgress != null) {
343353
final _req = http.Request('GET', Uri.parse(_url));
344354
_req.headers.addAll(header ?? {});
355+
if (!acceptRangesHeader)
356+
_req.headers[HttpHeaders.rangeHeader] = 'bytes=${buffer.length}-';
345357
final _res = await _req.send().timeout(timeoutDuration);
346-
List<int> buffer = [];
358+
acceptRangesHeader =
359+
_res.headers.containsKey(HttpHeaders.acceptRangesHeader) &&
360+
_res.headers[HttpHeaders.acceptRangesHeader] == 'bytes';
361+
if (!acceptRangesHeader) buffer.clear();
347362
final Completer<http.Response> completer = Completer<http.Response>();
348363
_res.stream.listen(
349364
(bytes) {

pubspec.yaml

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
name: flutter_advanced_networkimage
22
description: An advanced image provider provides caching and retrying for flutter app. Now with zoomable widget and transition to image widget.
3-
version: 0.5.0
3+
version: 0.5.1
44
author: "fuyumi <[email protected]>"
55
homepage: https://github.com/mchome/flutter_advanced_networkimage
66

77
dependencies:
88
flutter:
99
sdk: flutter
10-
http: "^0.12.0"
11-
path_provider: "^1.1.0"
12-
path: "^1.6.0"
13-
flutter_svg: "^0.13.0"
10+
http: ^0.12.0
11+
path_provider: ^1.2.0
12+
path: ^1.6.0
13+
flutter_svg: ^0.14.0
1414

1515
dev_dependencies:
1616
flutter_test:
1717
sdk: flutter
1818
test: "^1.6.0"
1919

2020
environment:
21-
sdk: ">=2.1.0 <3.0.0"
21+
sdk: ">=2.3.0 <3.0.0"
2222
flutter: ">=1.6.0 <2.0.0"

test/download_test.dart

+5-5
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ void main() {
4040
const Duration(milliseconds: 100),
4141
1.0,
4242
const Duration(seconds: 5),
43-
(_, v) => print(v),
43+
(_, v) => print(v.length),
4444
() => Future.value(realUrl),
4545
printError: true,
4646
),
@@ -59,20 +59,20 @@ void main() {
5959
const Duration(milliseconds: 100),
6060
1.0,
6161
const Duration(seconds: 5),
62-
(_, v) => print(v),
62+
(_, v) => print(v.length),
6363
() => Future.value(realUrl),
6464
printError: true,
6565
),
6666
result);
6767
});
68-
test('=> bad url', () async {
68+
test('=> bad url with skip 404 retry', () async {
6969
var url =
7070
'https://assets-cdn.github.com/images/modules/logos_page/GitHub-Mark.png';
7171

7272
expect(
73-
await loadFromRemote(url, null, 0, const Duration(milliseconds: 100),
73+
await loadFromRemote(url, null, 5, const Duration(milliseconds: 100),
7474
1.0, const Duration(seconds: 5), null, null,
75-
printError: true),
75+
skipRetryStatusCode: [404], printError: true),
7676
null);
7777
});
7878
test('=> not a url', () async {

0 commit comments

Comments
 (0)