load method

Future<Uint8List?> load(
  1. String? url
)

Downloads and caches image bytes for url.

Implementation

Future<Uint8List?> load(String? url) async {
  if (url == null || url.trim().isEmpty) return null;
  final key = url.trim();

  if (_memoryCache.containsKey(key)) {
    return _memoryCache[key];
  }
  if (_inFlight.containsKey(key)) {
    return _inFlight[key];
  }

  final completer = Completer<Uint8List?>();
  _inFlight[key] = completer.future;

  try {
    final response = await _dio.get<List<int>>(
      key,
      options: Options(responseType: ResponseType.bytes),
    );
    final raw = response.data;
    if (raw != null && raw.isNotEmpty) {
      final bytes = Uint8List.fromList(raw);
      _memoryCache[key] = bytes;
      completer.complete(bytes);
      return bytes;
    }
    completer.complete(null);
    return null;
  } catch (e) {
    if (kDebugMode) {
      debugPrint('PayOrc logo cache load failed for $key: $e');
    }
    completer.complete(null);
    return null;
  } finally {
    _inFlight.remove(key);
  }
}