Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix: Fix errorWidget on dart DDC (web) #950

Merged
merged 4 commits into from
Aug 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 58 additions & 25 deletions cached_network_image_web/lib/cached_network_image_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import 'package:cached_network_image_platform_interface'
import 'package:flutter/material.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';

enum _State { open, waitingForData, closing }

/// ImageLoader class to load images on the web platform.
class ImageLoader implements platform.ImageLoader {
@Deprecated('Use loadImageAsync instead')
Expand Down Expand Up @@ -115,39 +117,70 @@ class ImageLoader implements platform.ImageLoader {
int? maxWidth,
Map<String, String>? headers,
VoidCallback evictImage,
) async* {
) {
var streamController = StreamController<ui.Codec>();

try {
await for (final result in cacheManager.getFileStream(
final stream = cacheManager.getFileStream(
url,
key: cacheKey,
withProgress: true,
headers: headers,
)) {
if (result is DownloadProgress) {
chunkEvents.add(
ImageChunkEvent(
cumulativeBytesLoaded: result.downloaded,
expectedTotalBytes: result.totalSize,
),
);
}
if (result is FileInfo) {
final file = result.file;
final bytes = await file.readAsBytes();
final decoded = await decode(bytes);
yield decoded;
}
}
} on Object {
// Depending on where the exception was thrown, the image cache may not
// have had a chance to track the key in the cache at all.
// Schedule a microtask to give the cache a chance to add the key.
key: cacheKey,
);

var state = _State.open;

stream.listen(
(event) {
if (event is DownloadProgress) {
chunkEvents.add(
ImageChunkEvent(
cumulativeBytesLoaded: event.downloaded,
expectedTotalBytes: event.totalSize,
),
);
}
if (event is FileInfo) {
if (state == _State.open) {
state = _State.waitingForData;
}

event.file
.readAsBytes()
.then((value) => decode(value))
.then((data) {
streamController.add(data);
if (state == _State.closing) {
streamController.close();
chunkEvents.close();
}
});
}
},
onError: (e, st) {
scheduleMicrotask(() {
evictImage();
});
streamController.addError(e, st);
},
onDone: () async {
if (state == _State.open) {
streamController.close();
chunkEvents.close();
} else if (state == _State.waitingForData) {
state = _State.closing;
}
},
cancelOnError: true,
);
} on Object catch (e, st) {
scheduleMicrotask(() {
evictImage();
});
rethrow;
streamController.addError(e, st);
}
await chunkEvents.close();

return streamController.stream;
}

Future<ui.Codec> _loadAsyncHtmlImage(
Expand Down