loadImageData function IO

Future<HTMLImageElement?> loadImageData(
  1. String path,
  2. Loader loadingInfo
)

Asynchronously loads image data from the specified path into an HTMLImageElement.

  • path: The URL or path to the image file.
  • loadingInfo: A Loader instance to report loading progress and status.

Returns a Future that completes with the loaded HTMLImageElement on success, or null if loading fails (e.g., network error, invalid image).

Implementation

Future<HTMLImageElement?> loadImageData(String path, Loader loadingInfo) async {
  String? objectUrl;

  return load<HTMLImageElement?>(
    path,
    loadingInfo,
    responseType: "blob",
    defaultValue: null,
    onError: (event) {
      if (objectUrl != null) {
        URL.revokeObjectURL(objectUrl!);
      }
    },
    onLoad: (response, completer, onError) {
      final Blob? imageBlog = response as Blob?;
      if (imageBlog != null) {
        objectUrl = URL.createObjectURL(imageBlog);
        final img = HTMLImageElement();
        img.src = objectUrl!;
        img.addEventListener(
          'load',
          (Event event) {
            URL.revokeObjectURL(objectUrl!);
            completer(img);
          }.toJS,
        );
        img.addEventListener('error', onError.toJS);
      }
    },
  );
}