loadImageNetwork function

Future<ImageLoadResult> loadImageNetwork(
  1. String url
)

Loads and decodes an image from url for sampler uniforms.

Implementation

Future<ImageLoadResult> loadImageNetwork(String url) async {
  try {
    final uri = Uri.parse(url);
    final response = await http.get(uri).timeout(
      const Duration(seconds: 30),
      onTimeout: () => throw TimeoutException('Network request timeout'),
    );
    if (response.statusCode == 200) {
      final Uint8List bytes = response.bodyBytes;
      final codec = await ui.instantiateImageCodec(bytes);
      final frame = await codec.getNextFrame();
      return ImageLoadResult(image: frame.image);
    }
    final error = Exception('HTTP ${response.statusCode}');
    debugPrint('loadImageNetwork error: $url: $error');
    return ImageLoadResult(error: error);
  } on TimeoutException catch (e, stack) {
    debugPrint('loadImageNetwork timeout: $url: $e');
    return ImageLoadResult(error: e, stackTrace: stack);
  } catch (e, stackTrace) {
    debugPrint('loadImageNetwork error: $url: $e');
    debugPrintStack(stackTrace: stackTrace);
    return ImageLoadResult(error: e, stackTrace: stackTrace);
  }
}