preCacheImages<T, K> static method

Future<Map<String, Uint8List>> preCacheImages<T, K>(
  1. BuildContext context,
  2. List<TTableHeader<T, K>> headers,
  3. List<T> items
)

Pre-fetches images from network or assets for a list of items and headers.

Implementation

static Future<Map<String, Uint8List>> preCacheImages<T, K>(
  fm.BuildContext context,
  List<TTableHeader<T, K>> headers,
  List<T> items,
) async {
  final Map<String, Uint8List> cache = {};
  final effectiveHeaders = headers.where((h) => h.builder != null).toList();

  for (int i = 0; i < items.length; i++) {
    for (final header in effectiveHeaders) {
      try {
        final listItem = TListItem<T, K>(key: i as dynamic, data: items[i]);
        final widget = header.builder!(context, listItem, i);

        if (widget is TImage) {
          // Cache network image
          if (widget.url != null && !cache.containsKey(widget.url)) {
            try {
              final file = await DefaultCacheManager().getSingleFile(widget.url!);
              final bytes = await file.readAsBytes();
              if (bytes.isNotEmpty) {
                cache[widget.url!] = bytes;
              }
            } catch (_) {
              // If network fails, we'll try to fallback to placeholder in convert()
            }
          }

          // Cache placeholder asset
          if (!cache.containsKey(widget.placeholder)) {
            try {
              final assetPath = _getAssetPath(widget.placeholder);
              final ByteData data = await rootBundle.load(assetPath);
              cache[widget.placeholder] = data.buffer.asUint8List();
            } catch (_) {
              // Skip failing assets
            }
          }
        }
      } catch (_) {
        // Skip failing items
      }
    }
  }
  return cache;
}