flyerhq / flutter_link_previewer

Customizable link and URL preview extracted from the provided text with the ability to render from the cache. Ideal for chat applications.
MIT License
72 stars 75 forks source link

Bug when image is not accessible on Flutter Web #24

Closed wer-mathurin closed 3 years ago

wer-mathurin commented 3 years ago

Due to CORS issues when running on Flutter Web it may block at some point. This is not a problem in general if you use a cors-proxy. But there is on bug at least in the code:

In the Util.dart

Future<Size> _getImageSize(String url) {
  final stream = Image.network(url).image.resolve(ImageConfiguration.empty);
  final completer = Completer<Size>();

  void onError(Object e, StackTrace? __) {
    print(e);
  }

  void listener(ImageInfo info, bool _) {
    if (!completer.isCompleted) {
      completer.complete(
        Size(
          height: info.image.height.toDouble(),
          width: info.image.width.toDouble(),
        ),
      );
      stream.removeListener(ImageStreamListener(listener, onError: onError));
    }
  }

  stream.addListener(ImageStreamListener(listener, onError: onError));
  return completer.future;
}

The problem is that the completer never complete is it encounter a CORS problem for fetching the image. At least you can change the onError to:

void onError(Object e, StackTrace? s) {
    completer.completeError(e, s);
  }
demchenkoalex commented 3 years ago

Released in v2.2.2. Thanks for the PR!