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

chore: adding errorListener #777

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions cached_network_image/lib/src/cached_image_widget.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:cached_network_image/cached_network_image.dart';
import 'package:cached_network_image_platform_interface/cached_network_image_platform_interface.dart';
import 'package:cached_network_image_platform_interface/types.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
Expand Down Expand Up @@ -201,6 +202,10 @@ class CachedNetworkImage extends StatelessWidget {
/// Will resize the image and store the resized image in the disk cache.
final int? maxHeightDiskCache;

/// Function which is called after loading the image failed.
/// It prevents rethrow from being called and receives the error object.
final ErrorListener? errorListener;

/// CachedNetworkImage shows a network image using a caching mechanism. It also
/// provides support for a placeholder, showing an error and fading into the
/// loaded image. Next to that it supports most features of a default Image
Expand Down Expand Up @@ -234,6 +239,7 @@ class CachedNetworkImage extends StatelessWidget {
this.cacheKey,
this.maxWidthDiskCache,
this.maxHeightDiskCache,
this.errorListener,
ImageRenderMethodForWeb imageRenderMethodForWeb =
ImageRenderMethodForWeb.HtmlImage,
}) : _image = CachedNetworkImageProvider(
Expand All @@ -244,6 +250,7 @@ class CachedNetworkImage extends StatelessWidget {
imageRenderMethodForWeb: imageRenderMethodForWeb,
maxWidth: maxWidthDiskCache,
maxHeight: maxHeightDiskCache,
errorListener: errorListener,
),
super(key: key);

Expand Down
14 changes: 9 additions & 5 deletions cached_network_image/lib/src/image_provider/_image_loader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'dart:ui' as ui;
import 'dart:ui';

import 'package:cached_network_image_platform_interface/cached_network_image_platform_interface.dart';
import 'package:cached_network_image_platform_interface/types.dart';
import 'package:flutter/material.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';

Expand All @@ -27,7 +28,7 @@ class ImageLoader implements platform.ImageLoader {
int? maxHeight,
int? maxWidth,
Map<String, String>? headers,
Function()? errorListener,
ErrorListener? errorListener,
ImageRenderMethodForWeb imageRenderMethodForWeb,
Function() evictImage,
) {
Expand Down Expand Up @@ -56,7 +57,7 @@ class ImageLoader implements platform.ImageLoader {
int? maxHeight,
int? maxWidth,
Map<String, String>? headers,
Function()? errorListener,
ErrorListener? errorListener,
ImageRenderMethodForWeb imageRenderMethodForWeb,
Function() evictImage) {
return _load(
Expand Down Expand Up @@ -86,7 +87,7 @@ class ImageLoader implements platform.ImageLoader {
int? maxHeight,
int? maxWidth,
Map<String, String>? headers,
Function()? errorListener,
ErrorListener? errorListener,
ImageRenderMethodForWeb imageRenderMethodForWeb,
Function() evictImage,
) async* {
Expand Down Expand Up @@ -130,8 +131,11 @@ class ImageLoader implements platform.ImageLoader {
evictImage();
});

errorListener?.call();
rethrow;
if (errorListener != null) {
errorListener.call(e);
} else {
rethrow;
}
} finally {
await chunkEvents.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,20 @@ import 'dart:ui' as ui show Codec;

import 'package:cached_network_image_platform_interface/cached_network_image_platform_interface.dart'
show ImageRenderMethodForWeb;
import 'package:cached_network_image_platform_interface/types.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';

import 'cached_network_image_provider.dart' as image_provider;
import 'multi_image_stream_completer.dart';

import 'package:cached_network_image_platform_interface/cached_network_image_platform_interface.dart'
if (dart.library.io) '_image_loader.dart'
if (dart.library.html) 'package:cached_network_image_web/cached_network_image_web.dart'
show ImageLoader;

/// Function which is called after loading the image failed.
typedef ErrorListener = void Function();

/// IO implementation of the CachedNetworkImageProvider; the ImageProvider to
/// load network images using a cache.
class CachedNetworkImageProvider
extends ImageProvider<image_provider.CachedNetworkImageProvider> {
extends ImageProvider<CachedNetworkImageProvider> {
/// Creates an ImageProvider which loads an image from the [url], using the [scale].
/// When the image fails to load [errorListener] is called.
const CachedNetworkImageProvider(
Expand Down Expand Up @@ -49,7 +44,7 @@ class CachedNetworkImageProvider
final double scale;

/// Listener to be called when images fails to load.
final image_provider.ErrorListener? errorListener;
final ErrorListener? errorListener;

/// Set headers for the image provider, for example for authentication
final Map<String, String>? headers;
Expand All @@ -75,10 +70,10 @@ class CachedNetworkImageProvider
'load is deprecated, use loadBuffer instead, see https://docs.flutter.dev/release/breaking-changes/image-provider-load-buffer')
@override
ImageStreamCompleter load(
image_provider.CachedNetworkImageProvider key, DecoderCallback decode) {
CachedNetworkImageProvider key, DecoderCallback decode) {
final chunkEvents = StreamController<ImageChunkEvent>();
return MultiImageStreamCompleter(
codec: _loadAsync(key, chunkEvents, decode),
return MultiFrameImageStreamCompleter(
codec: _loadAsync(key, chunkEvents, decode).first,
chunkEvents: chunkEvents.stream,
scale: key.scale,
informationCollector: () sync* {
Expand All @@ -94,7 +89,7 @@ class CachedNetworkImageProvider
@Deprecated(
'_loadAsync is deprecated, use loadBuffer instead, see https://docs.flutter.dev/release/breaking-changes/image-provider-load-buffer')
Stream<ui.Codec> _loadAsync(
image_provider.CachedNetworkImageProvider key,
CachedNetworkImageProvider key,
StreamController<ImageChunkEvent> chunkEvents,
DecoderCallback decode,
) {
Expand All @@ -115,11 +110,11 @@ class CachedNetworkImageProvider
}

@override
ImageStreamCompleter loadBuffer(image_provider.CachedNetworkImageProvider key,
DecoderBufferCallback decode) {
ImageStreamCompleter loadBuffer(
CachedNetworkImageProvider key, DecoderBufferCallback decode) {
final chunkEvents = StreamController<ImageChunkEvent>();
return MultiImageStreamCompleter(
codec: _loadBufferAsync(key, chunkEvents, decode),
return MultiFrameImageStreamCompleter(
codec: _loadBufferAsync(key, chunkEvents, decode).first,
chunkEvents: chunkEvents.stream,
scale: key.scale,
informationCollector: () sync* {
Expand All @@ -133,7 +128,7 @@ class CachedNetworkImageProvider
}

Stream<ui.Codec> _loadBufferAsync(
image_provider.CachedNetworkImageProvider key,
CachedNetworkImageProvider key,
StreamController<ImageChunkEvent> chunkEvents,
DecoderBufferCallback decode,
) {
Expand Down
7 changes: 5 additions & 2 deletions cached_network_image/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ dependencies:
sdk: flutter
flutter_cache_manager: ^3.3.0
octo_image: ^1.0.0
cached_network_image_platform_interface: ^2.0.0
cached_network_image_web: ^1.0.2
cached_network_image_web:
git:
url: https://github.com/SuaMusica/flutter_cached_network_image.git
ref: feature/errorListener
path: cached_network_image_web
Comment on lines +12 to +16

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not completely sure, but should this be included in this PR?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need this to use the branch and compile.

When it's merged it should be removed



dev_dependencies:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ library cached_network_image_platform_interface;
import 'dart:async';
import 'dart:ui' as ui;

import 'package:cached_network_image_platform_interface/types.dart';
import 'package:flutter/material.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';

Expand Down Expand Up @@ -31,7 +32,7 @@ class ImageLoader {
int? maxHeight,
int? maxWidth,
Map<String, String>? headers,
Function()? errorListener,
ErrorListener? errorListener,
ImageRenderMethodForWeb imageRenderMethodForWeb,
Function() evictImage,
) {
Expand All @@ -49,7 +50,7 @@ class ImageLoader {
int? maxHeight,
int? maxWidth,
Map<String, String>? headers,
Function()? errorListener,
ErrorListener? errorListener,
ImageRenderMethodForWeb imageRenderMethodForWeb,
Function() evictImage,
) {
Expand Down
2 changes: 2 additions & 0 deletions cached_network_image_platform_interface/lib/types.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// Function which is called after loading the image failed.
typedef ErrorListener = void Function(dynamic);
11 changes: 6 additions & 5 deletions cached_network_image_web/lib/cached_network_image_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:cached_network_image_platform_interface'
import 'package:cached_network_image_platform_interface'
'/cached_network_image_platform_interface.dart'
show ImageRenderMethodForWeb;
import 'package:cached_network_image_platform_interface/types.dart';
import 'package:flutter/material.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';

Expand All @@ -27,7 +28,7 @@ class ImageLoader implements platform.ImageLoader {
int? maxHeight,
int? maxWidth,
Map<String, String>? headers,
Function()? errorListener,
ErrorListener? errorListener,
ImageRenderMethodForWeb imageRenderMethodForWeb,
Function() evictImage,
) {
Expand Down Expand Up @@ -56,7 +57,7 @@ class ImageLoader implements platform.ImageLoader {
int? maxHeight,
int? maxWidth,
Map<String, String>? headers,
Function()? errorListener,
ErrorListener? errorListener,
ImageRenderMethodForWeb imageRenderMethodForWeb,
Function() evictImage,
) {
Expand Down Expand Up @@ -87,7 +88,7 @@ class ImageLoader implements platform.ImageLoader {
int? maxHeight,
int? maxWidth,
Map<String, String>? headers,
Function()? errorListener,
ErrorListener? errorListener,
ImageRenderMethodForWeb imageRenderMethodForWeb,
Function() evictImage,
) {
Expand Down Expand Up @@ -119,7 +120,7 @@ class ImageLoader implements platform.ImageLoader {
int? maxHeight,
int? maxWidth,
Map<String, String>? headers,
Function()? errorListener,
ErrorListener? errorListener,
Function() evictImage,
) async* {
try {
Expand All @@ -146,7 +147,7 @@ class ImageLoader implements platform.ImageLoader {
evictImage();
});

errorListener?.call();
errorListener?.call(e);
rethrow;
} finally {
await chunkEvents.close();
Expand Down
6 changes: 5 additions & 1 deletion cached_network_image_web/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ dependencies:
flutter:
sdk: flutter
flutter_cache_manager: ^3.0.0
cached_network_image_platform_interface: ^2.0.0
cached_network_image_platform_interface:
git:
url: https://github.com/SuaMusica/flutter_cached_network_image.git
ref: feature/errorListener
path: cached_network_image_platform_interface

dev_dependencies:
flutter_test:
Expand Down