rasterizeSvgAsset function

Future<Uint8List?> rasterizeSvgAsset(
  1. String assetPath, {
  2. int size = defaultSvgRasterSize,
})

Rasterizes the Flutter asset SVG at assetPath into PNG bytes.

The output is a square image of size x size logical pixels. Results are cached in-memory keyed by assetPath|size, so repeated calls return the same Uint8List instance. Concurrent calls for the same asset/size share a single in-flight operation.

Returns null when the asset cannot be loaded or rasterized (e.g. invalid SVG, missing asset).

Implementation

Future<Uint8List?> rasterizeSvgAsset(
  String assetPath, {
  int size = defaultSvgRasterSize,
}) {
  final cacheKey = '$assetPath|$size';

  final cached = _svgRasterCache[cacheKey];
  if (cached != null) return Future.value(cached);

  final inflight = _svgRasterInflight[cacheKey];
  if (inflight != null) return inflight;

  final operation = _rasterize(assetPath, size, cacheKey);
  _svgRasterInflight[cacheKey] = operation;
  return operation.whenComplete(() => _svgRasterInflight.remove(cacheKey));
}