resolveSvgInPayload function
Recursively walks a method-channel node (maps/lists), rasterizing any
Flutter asset SVG referenced by an image-bearing key (see svgImageDataKeys)
and attaching the PNG bytes to a sibling <key>Data key.
Behavior:
- A single-image key (e.g.
image,imageUrl) whose value is an SVG asset -> sibling<key>Data(Uint8List). - A list-image key (e.g.
gridImages) -> sibling<key>Data(List<Uint8List?>,nullfor non-SVG entries). - Keys in svgIgnoredKeys are skipped entirely (never rasterized, never recursed into).
- Original image strings are preserved for native fallback / back-compat.
The node is mutated in place and also returned for convenience.
Implementation
Future<dynamic> resolveSvgInPayload(
dynamic node, {
int size = defaultSvgRasterSize,
}) async {
if (node is Map) {
for (final entry in svgImageDataKeys.entries) {
final value = node[entry.key];
if (svgListImageKeys.contains(entry.key)) {
if (value is! List) continue;
var hasSvg = false;
final data = <Uint8List?>[];
for (final item in value) {
final bytes = await _rasterizeIfSvg(item, size);
data.add(bytes);
if (bytes != null) hasSvg = true;
}
if (hasSvg) node[entry.value] = data;
} else {
final bytes = await _rasterizeIfSvg(value, size);
if (bytes != null) node[entry.value] = bytes;
}
}
// Recurse into all values, skipping ignored keys and the byte payloads we
// just attached. The latter are raster bytes ([Uint8List], which is itself
// a `List<int>`) or lists of them; descending into those would needlessly
// walk every individual byte.
for (final key in node.keys.toList()) {
if (svgIgnoredKeys.contains(key)) continue;
if (_svgDataKeys.contains(key)) continue;
final value = node[key];
if (value is Uint8List) continue;
await resolveSvgInPayload(value, size: size);
}
} else if (node is List) {
for (final item in node) {
if (item is Uint8List) continue;
await resolveSvgInPayload(item, size: size);
}
}
return node;
}